Skip to content

Conversation

@nordic-krch
Copy link
Contributor

@nordic-krch nordic-krch commented Mar 5, 2019

This PR contains 3 commits:

  • adding cmock repository to ncs (extending west configuration).
  • adding files (boilerplate, cmakelists, kconfig, etc) which enables adding unity tests.
  • adding example (tests/unity/example_test) to show how unit tests can be created.

Open items:

  • currently cmock repository is taken directly from https://github.com/ThrowTheSwitch. Should we fork it to nordic playground and take it from there?
  • currently cmock and unity CMakeLists.txt is created in nrf/tests/unity and it points to the sources in external repository (this way repository is unspoiled). Any better proposal?

Please note that this is my first PR to NCS. Let me know if i did something wrong.
Please note that i'm total newbie to cmake so be patient in case of rookie mistakes.

@CLAassistant
Copy link

CLAassistant commented Mar 5, 2019

CLA assistant check
All committers have signed the CLA.

@NordicBuilder
Copy link
Contributor

NordicBuilder commented Mar 5, 2019

All checks are passing now.

Review history of this comment for details about previous failed status.
Note that some checks might have not completed yet.

@nordic-krch nordic-krch force-pushed the unity_cmock branch 2 times, most recently from cd397ef to fd404e3 Compare March 8, 2019 07:15
@lemrey
Copy link
Contributor

lemrey commented Mar 8, 2019

@nordic-krch fantastic! Are there any requirements in terms of tooling for this, e.g. Ruby, Python modules?

@nordic-krch
Copy link
Contributor Author

@lemrey, right. Ruby is of course required. In python no additional lib is needed.

@lemrey lemrey requested a review from carlescufi March 8, 2019 09:00
@nordic-krch
Copy link
Contributor Author

@SebastianBoe @tejlmand could you take a look as this is mainly cmake stuff and i'm newbie there.
fyi @gbakke

@SebastianBoe
Copy link
Contributor

regex parsing of C?

:(

I'm concerned that we won't be able to regex parse the full language of CPP+CC, and that what we have here is just able to parse a subset of the language.

What happens when the parsing fails, can we create mocks manually for the cases that are not covered?

Sorry if this question doesn't make sense, I'm not familiar with mocking frameworks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you just need the func_names? Not the full prototype?

If so, maybe it would be possible to compile the header and then take the func names from the symbol table.

Then we don't need to regex parse unpreprocessed C code.

Copy link
Contributor

@SebastianBoe SebastianBoe Mar 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is not guaranteed to be closed, use 'with' instead of manually invoking close().

@nordic-krch
Copy link
Contributor Author

@SebastianBoe it is parsing header files mainly in search for function declarations and static inline functions. We need to handle following:

  • static inline functions cannot be mocked (because implementation is already provided in the header)
  • zephyr specific function declaration in case of syscall
  • wrapping around calls to allow mocking without removing mocked functions from the binary.

So in regexp we do not go beyond function declarations and we are focused on C since Cmock/Unity is C unit test framework.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print() is best-practice

@SebastianBoe
Copy link
Contributor

we are focused on C

Sorry, by CPP, I meant C preprocessor.

@nordic-krch nordic-krch force-pushed the unity_cmock branch 2 times, most recently from 8ab1436 to d4b088f Compare March 14, 2019 11:57
@nordic-krch
Copy link
Contributor Author

@SebastianBoe i've applied your comments. Regarding compiling header file: I'm not sure how to do that but imo, it can be later on improved if we find issues with using python to parse the header.

What happens when the parsing fails, can we create mocks manually for the cases that are not covered?

In case regexp is missing something and we find case which fails we need to improve regexp.

@SebastianBoe
Copy link
Contributor

In case regexp is missing something and we find case which fails we need to improve regexp.

What if it's impossible?

e.g. if the prototype is constructed by the preprocessor?

@nordic-krch
Copy link
Contributor Author

@SebastianBoe, worst case function won't be mocked. Header processed by python next goes to cmock ruby script which processes it in search for function prototypes. It will create mocks only for found prototypes. Python script can only improve.

@SebastianBoe
Copy link
Contributor

Okay, conceptually this makes sense then.

I can review the CMake code when the PR is in PR-form and no longer an RFC.

@nordic-krch nordic-krch changed the title [RFC] Add Unity and CMock to NCS Add Unity and CMock to NCS Mar 14, 2019
@nordic-krch
Copy link
Contributor Author

nordic-krch commented Mar 14, 2019

i've put RFC since there were some open items mentioned in the PR but i see no opinions so far and those open items can be fixed later (like changing west to use unity fork instead original) so removing RFC.

@SebastianBoe
Copy link
Contributor

@gbakke , @hakonfam : FYI

@SebastianBoe
Copy link
Contributor

currently cmock repository is taken directly from https://github.com/ThrowTheSwitch. Should we fork it to nordic playground and take it from there?

I'd postpone this until necessary.

Copy link
Contributor

@SebastianBoe SebastianBoe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First pass of review done, was not able to get through all of it today.

@nordic-krch nordic-krch force-pushed the unity_cmock branch 3 times, most recently from 8046051 to ca2b63d Compare March 18, 2019 07:07
@nordic-krch
Copy link
Contributor Author

@ru-fu please not that i've just updated documentation and removed last paragraph.

Copy link
Contributor

@tejlmand tejlmand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found a minor thing in python scripts.
Zephyr is supporting python >= 3.4

Copy link

@ru-fu ru-fu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a commit with doc update. Please check and squash if okay.

@nordic-krch
Copy link
Contributor Author

@ru-fu thanks for review and changes. They look ok, i've squashed your commit.

@nordic-krch nordic-krch force-pushed the unity_cmock branch 3 times, most recently from d7fcdd9 to 175a1bd Compare March 28, 2019 10:00
@nordic-krch
Copy link
Contributor Author

@tejlmand follow-up issue created. sample.yaml created. Can you re-check?

Copy link
Contributor

@tejlmand tejlmand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good :)

@nordic-krch
Copy link
Contributor Author

@carlescufi i think we can merge it. Cmake experts are done, as well as the documentation.

@carlescufi
Copy link
Contributor

@nordic-krch very well, can you rebase on top of master?

@nordic-krch
Copy link
Contributor Author

@carlescufi rebased.

@carlescufi
Copy link
Contributor

@nordic-krch still shows conflicted, probably someone has merged stuff in the meantime.

Actions performed to add unity/cmock to ncs:
- Add cmock (unity as submodule) repository to west list of projects
- Add CMakeLists.txt file in nrf/tests/unity
- Add Unity Kconfig file to nrf/tests/unity

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
This commis adds tests/unity/unity_test.cmake file which includes
standard zephyr boilerplate and on top of that adds functions for
creating mocks and test runner. Additionally, two helper python
scrips are added. Scripts helps to handle: mocking static inline
functions, mocking using --wrap linker feature, skipping syscalls
used in zephyr API's.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
Converted NRF_DIR to cache variable, in order to allow access to it
from any location.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
Created unity library which contains functions for cmock and
runner generation.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
Example shows how to:
- mock a module (see CMakeLists.txt)
- generate mock and test runner (see CMakeLists.txt)
- use mock in the test (example_test.c)
- override IS_ENABLED macro to runtime test compile time features
  (preincluded example_test.h)

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
Added user guide which explains how to create and run unit
test using Unity and CMock.

Signed-off-by: Ruth Fuchss <ruth.fuchss@nordicsemi.no>
Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
@nordic-krch
Copy link
Contributor Author

@carlescufi can you merge now?

@carlescufi carlescufi merged commit 730cc40 into nrfconnect:master Apr 1, 2019
@@ -0,0 +1,17 @@
# Kconfig - Config options for logger sample app
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the comment is a bit off.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants