7 years, 8 months ago.

How do we use mbed os 5 + cli for TDD?

Hi,

Under known issues in the mbed os 5 / mbed cli documentation it says:

There cannot be a main() function outside of a TESTS directory when building and running tests. This is because this function will be included in the non-test code build as described in the Building process section. When the test code is compiled and linked with the non-test code build, a linker error will occur due to their being multiple main() functions defined. For this reason, please either rename your main application file if you need to build and run tests or use a different project.

I would just like to confirm there is currently no way to setup a project that also has tests inside of it. If so, could someone please explain how to actually use the "mbed test" command as it would seem that it does not support applications with a main function at all, only libraries?

Is there some sort of project structure I can adopt to mitigate this issue

Documentation seems to offer no work around and there are no project structure examples. The new test feature is very exciting

2 Answers

7 years, 8 months ago.

Hi Kyle - A workaround would be to rename the application file which contains a main function with a *.txt or similar filetype that will not be included in compilation. The plan is to support tests nested within an application soon.

Accepted Answer

I suspect I would need to do this each time I run mbed test and undo each time I want to compile. I look forward to support for nested tests, however unfortunately it won't land in time for me to take advantage of it on this project

posted by Kyle Van Berendonck 23 Aug 2016

Do you know if it's possible to set a flag/preprocessor directive from the command line when running tests, then it would be possible to chop main out with directives

posted by Kyle Van Berendonck 23 Aug 2016

Nice idea :) will have to look at how we hook main across different tools and libraries to see how that works

posted by Sam Grove 23 Aug 2016

I did some digging of my own and found that there is an undocumented feature where mbed test will accept a -D argument like so: mbed test -t GCC_ARM -m LPC1768 -DMBED_TESTSUITE=1 . With this in mind, I simply wrapped my main() function in #ifndef MBED_TESTSUITE .... #endif and was able to correct the whole issue rather painlessly. This might be a good workaround to put on the wiki.

posted by Kyle Van Berendonck 23 Aug 2016

Just now having the issue that mbed test passes all arguments across to mbedgt/greentea and it doesn't recognize the -D, so I can build the test suite but the automatic runner is not working.

posted by Kyle Van Berendonck 24 Aug 2016

For anyone digging this thread up later, I fixed this by creating a batch file like follows. If all your tests are named like myprefix-somegroup-sometest then tests-myprefix will distinguish them from the massive mbed test suite when running tests. The wildcard seems to be another undocumented feature. The separate compile and run are necessary because the mbed running tool errors out if you pass it -D which seems to happen for some reason with the MBED_TESTSUITE macro which is intended to nop out the main() function in main.cpp. The file touches main.cpp each time just to make sure it is rebuilt

#!/bin/sh
RED='\033[0;31m'
NC='\033[0m'

touch main.cpp
OPTIONS='-n tests-myprefix* -t GCC_ARM -m LPC1768'

printf "${RED}⇒ Building testsuite${NC}\n"
mbed test --compile ${OPTIONS} -DMBED_TESTSUITE=1

printf "\n${RED}⇒ Running testsuite${NC}\n"
mbed test --run ${OPTIONS}
posted by Kyle Van Berendonck 24 Aug 2016
-deleted-
7 years, 8 months ago.

Hi Kyle,

Thanks for bringing this up. This is partially covered in the mbed CLI documentation:

Perhaps it's not clear that the 'TESTS' directory has a special meeting/function and applies to library and application level tests as well.

Please let me know if the information above was helpful.

Cheers,
Mihail

Hi Mihail, this is clear enough however the documentation steps do not actually work and throw an error when run on an application as mentioned in the Known Issues section. I haven't seen a work around that doesn't involve renaming files containing the original main function to avoid the linker error

posted by Kyle Van Berendonck 23 Aug 2016