prodcode.h
---------------
#pragma once
void doit();
prodcode.cpp
------------------
#include "prodcode.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void doit(){
::open("abc.txt", O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
}
test.cpp
----------
#include <CppUTest/TestHarness.h>
#include <CppUTest/CommandLineTestRunner.h>
#include <CppUTestExt/MockSupport.h>
#include "prodcode.h"
TEST_GROUP(SomeTest) {
void teardown()
{
mock().clear();
}
};
extern "C"
{
int open(const char * path, int mode, ...)
{
mock().actualCall("open")
.withParameter("path", path)
.withParameter("mode", mode);
return 11;
}
}
TEST(SomeTest, callingOpen) {
mock()
.expectOneCall("open")
.withParameter("path", "abc.txt")
.withParameter("mode", O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC)
.andReturnValue(11);
doit();
mock().checkExpectations();
}
int main(int argc, char** argv)
{
return CommandLineTestRunner::RunAllTests(argc, argv);
}
After issuing "g++ *.cpp -fprofile-arcs -ftest-coverage -lCppUTest -lCppUTestExt" I tried to run the executable and get:
.
OK (1 tests, 1 ran, 1 checks, 0 ignored, 0 filtered out, 0 ms)
unknown file:0: error: Failure in TEST(
NOTE: Assertion happened without being in a test run (perhaps in main?),
Something is very wrong. Check this assertion and fix)
Mock Failure: Unexpected call to function: open
EXPECTED calls that did NOT happen:
<none>
ACTUAL calls that did happen (in call order):
<none>
terminate called after throwing an instance of 'CppUTestFailedException'
Aborted (core dumped)