any tricks to help us from forgetting to call test functions?

S

Siemel Naran

Hi. I have a file of test functions.

int test1() {
return 0;
}
int test2()
return 0;
}

There is another function that calls all the tests.

int doall() {
test1();
test2();
}

Problem is that if I add more tests, I may forget to update doall. Anyone
have any tricks to help me remember to update doall? (Of course, I can test
my code thoroughly, but I'd rather catch silly omissions earlier with the
compiler's help.)

Here is my present trick, but it uses macros.


#include <vector>
#include <numeric>


std::vector<int (*)()> testfuncs;
struct AddTest
{
AddTest(int (*func)())
{
testfuncs.push_back(func);
}
};

#define NEW_TEST(name) int name(); \
AddTest AddTest_##name(&name); \
int name()

struct callfunc
{
int operator()(int previousresult, int (*func)()) const
{
int newresult = (*func)();
return newresult + previousresult;
}
};

////////////////////////////////////////////////////////////////////////////
////

NEW_TEST(test1) {
return 0;
}


NEW_TEST(test2) {
return 0;
}


int doall()
{
int out = 0;
std::accumulate(testfuncs.begin(), testfuncs.end(), out, callfunc());
return out;
}
 
P

Phlip

Siemel said:
NEW_TEST(test1) {
return 0;
}


NEW_TEST(test2) {
return 0;
}


int doall()
{
int out = 0;
std::accumulate(testfuncs.begin(), testfuncs.end(), out, callfunc());
return out;
}

Mike Feathers and I have that pattern:

http://www.xpsd.org/cgi-bin/wiki?NanoCppUnit

BTW it's not a matter of "forgetting", if you start each feature with a
failing test, because if nothing fails you will realize your test didn't
call. It's a matter of unifying the point of change in a program.

Now, before you read the link, how would you pass a test suite, with
setUp(), tearDown(), and member variables into that macro?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top