Skip to content

Instantly share code, notes, and snippets.

@CrockAgile
Last active April 29, 2016 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CrockAgile/14e5502de89ab8de9fee9782256538c1 to your computer and use it in GitHub Desktop.
Save CrockAgile/14e5502de89ab8de9fee9782256538c1 to your computer and use it in GitHub Desktop.
Cgreen Introduction
git clone https://github.com/cgreen-devs/cgreen.git
cd cgreen
make -j4
make test
make install
# hacky fix since default install to lib64 for some reason
# open to suggestions of better solution
# append "/usr/local/lib64" to ld config
sudo vim /etc/ld.so.conf
sudo ldconfig
#include <cgreen/cgreen.h>
// What is the subject under test? SUT
// Only one per test file
Describe(ncom_message);
// Unfortunately required even if unused
BeforeEach(ncom_message)
{}
AfterEach(ncom_message)
{}
Ensure(ncom_message, is_correct_size)
{
ncom_message test_message;
assert_that(
sizeof(test_message),
is_equal_to(NCOM_MSG_SIZE) );
}
// repeat for other tests
#include <cgreen/cgreen.h> // general unit testing
#include <cgreen/mocks.h> // mocking functionality
#include "oxts_rt_driver.h"
// Declares "oxts_rt_configure_socket" which uses "psync_socket_bind".
// Mocks and stubs help to isolate our unit tests from
// other functions, objects, and the system.
// "psync_socket_bind" is not what we are testing,
// so instead we will create a replacement mock function.
int psync_socket_bind(
ps_socket * const sock )
{
// "mock" is a special function that returns values
// based on expectations provided in the tests below
return (int)mock(sock);
}
// global test object recycled for each test
ps_socket test_socket;
Describe(oxts_socket);
BeforeEach(oxts_socket)
{
memset( &test_socket, 0, sizeof(test_socket) );
}
AfterEach(oxts_socket)
{}
Ensure(oxts_socket, configuration_calls_bind)
{
// expect that psync_socket_bind will be called once (no more)
// with the socket provided and will return DTC_NONE
expect( psync_socket_bind,
when(sock, is_equal_to( &test_socket )),
will_return(DTC_NONE) );
assert_that(
oxts_rt_configure_socket( &test_socket, "10.10.10.10", 1000 ),
is_equal_to(DTC_NONE) );
}
# same as all makefiles
# tests can be built as classic objects and linked
# but Cgreen provides its own convenient "cgreen-runner"
# which will find and run all Cgreen tests in a .so
TEST_RUNNER := test/test-oxts-rt-driver.so
TEST_SRCS := $(wildcard test/*.c)
test: $(TEST_RUNNER)
cgreen-runner --colours $<
# test files are linked before libraries, which allows a mock
# version of any function in the library to be substituted
$(TEST_RUNNER): $(TEST_SRCS) $(TARGET)
$(CC) $(CCFLAGS) $(INCLUDE) -shared -o $@ $^ $(LIBS) -lcgreen
@shnewto
Copy link

shnewto commented Apr 28, 2016

For those of use with lenovo laptops the path /usr/local/lib/x86_64-linux-gnu/ needs to be added to /etc/ld.so.conf rather than /usr/local/lib64

Also I needed to run updatedb after install

@shnewto
Copy link

shnewto commented Apr 29, 2016

This needs to be added to 4-Makefile:

# moc_target shared library file
MOC_TARGET  := tests/libps_neobotix_usboard_moc_driver.so

# moc-target 
$(MOC_TARGET): $(OBJS)
    $(CC) -shared $(CCFLAGS) -o $@ $^

Then revise the TEST_RUNNER target to depend on MOC_TARGET instead of TARGET

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment