ThrowTheSwitch / CMock

CMock - Mock/stub generator for C
http://throwtheswitch.org
MIT License
684 stars 276 forks source link

Is it possible to use pytest + cmock? #389

Closed LinusLing0910 closed 8 months ago

LinusLing0910 commented 2 years ago

Current I can used the ceedling + cmock + unity for the ubit testing for my project, it is great.

But I need to do end to end testing (function testing) for embedded software testing, the embedded software testing maybe from both C and Rust, they will provide me the dynamic library, is it possible to use cmock for the C part, and manage the tests using pytest?

Or any suggestions for the sistuation?

Letme commented 2 years ago

Depends what your definition of possible is.

Pytest can run anything Python, so you can wrap whole Ceedling command line execution into a subproces call and then simply execute that wrapper (test) with pytest. It will call Ceedling and return proper error value when something fails. If you are considering to scratch out Ceedling, the CMock (and compiler) can be invoked via the command line (makefile) and executed via python (again, invoking subprocess). The fun part might be Scons (https://scons.org/), which is python based build system, so if you want to dump Ruby out of your stack. In Scons case you will probably want to invoke pytest via the Scons (not the other way around).

You can also replace unity with some other unit test framework - you just need a wapper, or even more proper would be to create a plugin for CMock (which is now defaulting to Unity). Here is how you invoke CMock from makefile (be careful I use _mock as postfix - but you will get the idea)

RUBY ?= $(shell which ruby)
MOCK_C_FILES := $(H_FILES:%.h=%.c)
MKDIR = mkdir -p
HIDE_CMD = @
NO_OUTPUT = > /dev/null

COLOR_VIOLET = echo -n "\033[0;35m";
COLOR_RESET = echo -n "\033[0m"; # Reset terminal colour to normal.

$(MOCK_C_FILES):$(SRCDIR)/$(TARGET_MOCKS_DIR)/%_mock.c: $(SRCDIR)/src/%.h
    $(COLOR_VIOLET) @echo " - $< --> $@"; $(COLOR_RESET)
    $(HIDE_CMD)$(MKDIR) $(dir $@)
    $(HIDE_CMD)$(RUBY) cmock/lib/cmock.rb --mock_path=$(TARGET_MOCKS_DIR) -o$(CMOCK_CONFIG) $< $(NO_OUTPUT)