ThrowTheSwitch / Ceedling

Ruby-based unit testing and build system for C projects

Home Page:http://throwtheswitch.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Test Case Naming Convention

swaldhoer opened this issue · comments

I took me something like 20 minutes to realize why the test case
 

extern void testMyTest(void){}

 
wouldn't run although on first sight it looks like it fulfills the requirements defined in Convention for Test Case Functions + Test Runner Generation.
 
However, it seems to be invalid to explicitly specify the function scope via extern.
 
I would not call it a typical bug, but this behavior was quite unexpected.
Maybe both
 

  • extern void testMyTest(void) and
  • void testMyTest(void)
     
    should be valid ways to specify a test function.

No way. Read more about extern and put it only where and when it is needed. It will be a good lesson in C.

extern is not even to be abused in header files of compilation units for declarations, which are accessible. It is only to be used when something is to be exposed and implemented out of scope. So practically, example.h should not have extern for functions defined in example.c.

I have never seen such example, even if you quote MISRA for usage of "extern" (although read newer MISRA to get more details what they really meant).

example.h has extern keyword, which is useless in this case in header file:

extern void testFunc(void);

This is called declaration, so you declared this function is external to the compilation unit. Since example.h is included in many files, this seems quite strange to declare it has externally defined function, especially if you provide definition for this function along with it - not that user has to make his/her own definition in another compilation unit not related to yours. That is actually the case extern should be used.

So let's follow your example where we define a function in our example.c compilation unit. In example.c has the testFunc is in all my cases defined without extern:

#include "example.h"

void testFunc(void)
{
    // bla
}

I have never seen source file with extern keyword like you noted above:

#include "example.h"

extern void testFunc(void)
{
    // bla
}

So maybe since you know MISRA you are more experienced than what I expected, but how would you/compiler read this chunk of code? This function is external to this compilation unit, but here it is defined? So is it external or not? Why would you add extern next to the function definition?

@swaldhoer Could you say more about how you are arranging your test cases that inspired the use of extern? Are you, for instance, trying to arrange multiple tests suites with certain test cases in common? The typical Ceedling test suite comprises multiple test executables where each test executable maps to / is built from a single C file of test cases.