ThrowTheSwitch / CMock

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

how do i use cmock? #413

Closed MJ-CJM closed 1 year ago

MJ-CJM commented 2 years ago

hi: I already know how cmock work (https://www.throwtheswitch.org/cmock),but i don't know how to uses Ruby scripts to auto-generate C source modules conforming to the interfaces specified in your C header files,Maybe it's my first exposure to ruby,do we have such guidance documents?

Letme commented 2 years ago

Here is an excerpt from our makefile. The cmock submodule is located in $(UNITTEST_FRAMEWORK)/tools/. and configuration yaml is passed through CMOCK_CONFIG. HIDE_CMD and NO_OUTPUT can simply be @ and /dev/null so that we have less printouts.

$(MOCK_COMPONENT_C_FILES):$(SRCDIR)/$(MOCKS_DIR)/%_mock.c: $(SRCDIR)/src/%.h
    $(HIDE_CMD)$(MKDIR) $(dir $@)
    $(HIDE_CMD)$(RUBY) $(UNITTEST_FRAMEWORK)/tools/cmock/lib/cmock.rb --mock_path=$(MOCKS_DIR) -o$(CMOCK_CONFIG) $< $(NO_OUTPUT)
Letme commented 2 years ago

There are also examples that can help: https://github.com/ThrowTheSwitch/CMock/blob/master/examples/make_example/Makefile

Letme commented 2 years ago

Maybe also https://github.com/ThrowTheSwitch/CMock/issues/389#issuecomment-1085576545 might be helpful if you are looking into non-ceedling path.

MJ-CJM commented 2 years ago

Thanks a lot, I'm trying

MJ-CJM commented 1 year ago

Thank you very much, I have done the test of the case according to the makefile,In addition, I have tow questions

  1. can I test by modifying the automatically generated .build/test/mocks/ files ?
  2. can I not pass the automatically generated mock file, but write the mock function file to test?
Letme commented 1 year ago

Can you rephrase your questions?

  1. Why would you modify automatically generated mock files? They will be overwritten on each build when a new function is added and are part of the build process so they should not be committed to version control (as an example).
  2. That is up to your build system (are you using Ceedling?) - in the most general cases in your test file you do not include the mock_myfile.h, and include a real header file (myfile.h) which will get linker complaining about missing functions, so you need to write bodies of the functions in the file. Secondly, just split out the functions you don't want to mock in another header file and work with it that way.
MJ-CJM commented 1 year ago

Thank you very much for your reply, I haven't used ceedling yet, but only through unity and cmock to complete the test. Is this also a feasible solution? Are there any good ceedling examples that I can learn from?

Letme commented 1 year ago

Throwtheswitch website is usually a decent starting point: https://www.throwtheswitch.org/ceedling

Otherwise GitHub has some basic examples there as well: https://github.com/ThrowTheSwitch/Ceedling/tree/master/examples

Ceedling basically ties together Unity and Cmock, so that you do not need to write your own build system. It is great for basic and advanced use, the only drawback is that it is written in Ruby in case you will want to implement some new feature.

Please close the issue if it has been resolved.