Need some help in understanding UnitTest C++ code

Started by
9 comments, last by Paradigm Shifter 10 years, 6 months ago

Hello,
I am trying to understand the following code written in unix platform about unitTest:

/****************************************/
#define TEST(name,classUnderTest)
class classUnderTest##name##Test : public Test
{
public:
classUnderTest##name##Test () : Test (#name "Test") {}
void setup() {};
void teardown() {};
void runTest (TestResult& result_);
} classUnderTest##name##Instance;
void classUnderTest##name##Test::runTest (TestResult& result_)

/*****************************************************/

Could anyone explain what does ##name## stand for? I would like to use in visual studio 2010.

Please provide some suggestion as I am a newbie in writing unitTest.


Thanks in advance,

Advertisement

The ## operator is the preprocessor token concatenation operator; TEST(MyClass, ...) expands the first line to class classUnderTestMyClassTest : public Test. It is necessary to concatenate substrings in a macro to form a single token without white spaces between the subtokens. It simply concatenates both sides of the operator to a single new token.

Yeah thats just macro-magic to provide you with an easy means of writing your own tests. You better just look at the documentation of the library on how to write tests for your code and not inside the header files where the magic happens that makes it possible.

Thanks for the reply. But if I paste this code in visual studio 2010 i found red error line. What am I missing? Please let me know. Thanks.

It seems to be missing the backslash at the end of each line to continue the macro definition on the next line (I assume it was sanitized out by the forum software, or some other medium between its origin and where we see it now), and the last line appears to be where the runTest() method is supposed to be defined, but the function has no body. That's my first guess.

You dont paste this code into your project. I cant see from your posting which test-framework you actually use, but you just use the provided macros to write your tests, similar to this:


#include "my_functions.h"

#include "unittestpp.h"

SUITE(my_function_tests) {

TEST(my_function_one) {
  CHECK_EQUAL(3,my_function(1))
}

// more tests here

}

Thank you all for your clarification. I have the following questions as shown with circle and tick mark in the figure, it would be very kind of you to clarify the questions in the light of the figure:

1. “name##Setup” class has not yet been defined. How that class can be inherited by “class classUnderTest##name##Test”? Please give some clarification.

2. What does the encircled line 2 (i.e. #name “Test”) mean?

3. What does the encircled line 3 imply? (name##Setup::setup()) as name##Setup class has notyet been defined?

4. At marking 4, “classUnderTest##name##Instance”, is it an instance of “classclassUnderTest##name##Test”, usually this kind of definition is used while writing stuct.

5. In 5, the function “runTest” has again been defined, why?

6. Why are there so many back slashes? How long is the macro definition ?

Please help me clarify the conception. Thanks in advance.

Thats mostly quirks of the C preprocessor; you can think of it as a simple program doing text-replacement before compiling.

6. A #define only extends to the end of line, but that macro is so long it needs many lines, thats why there is the line continuation operator \ on all those lines.

2. The # operator changes the macro argument into a string, such that after the preprocessor runs this code:


#define M(ARGUMENT) #ARGUMENT

M(hello)

gets transformed into:


"hello"

and then the two strings in your example get automatically concatenated.

- ## is the token concatenation operator which transforms this:


#define M(x) whatever##x

M(itis)

into


whateveritis

after preprocessing.

5. Thats the start of the testfunction you write, the body you would place behind the macro invocation, like this:


TESTWITHSETUP(mytest,testedClass) {
  // put your testcode here
}

4. Thats probably put there as a short way to define a global variable of that test class in the macro.

1.+3. For that you better read the documentation for that test framework, as I already suggested. Maybe you have to invoke some other macro to define that before. But I still dont understand why you want to know all these implementation details when you just want to use the test framework to write test cases for your code. Or do you want to sink time into writing your own test framework although you seem to have one already you can use?

Hi, thanks for the reply. Actually I am trying to run a simple unit test project (that I have downloaded from the net) to test how it works as I need to write some code for unit test in C++. I have attached the zipped folder. I can compile the project in visual studio 2010, but while running, i get the following error:

/******************************************************************************************************/
fixture_test.obj : error LNK2019: unresolved external symbol "public: __thiscall Test::Test(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??0Test@@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: __thiscall Test1MyFixtureTest::Test1MyFixtureTest(void)" (??0Test1MyFixtureTest@@QAE@XZ)
1>simplest_test.obj : error LNK2001: unresolved external symbol "public: __thiscall Test::Test(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??0Test@@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
1>fixture_test.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Test::run(class TestResult &)" (?run@Test@@UAEXAAVTestResult@@@Z)
1>simplest_test.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Test::run(class TestResult &)" (?run@Test@@UAEXAAVTestResult@@@Z)
1>C:\Users\MYG741\Documents\Visual Studio 2010\Projects\Unit_test\Debug\Unit_test.exe : fatal error LNK1120: 2 unresolved externals


/******************************************************************************************************/

I don't understand what the problem is. It would be very kind, it some help me figure out what's going wrong. My goal is to learn to write unit test in C++. I don't want to use any external libraries. Please provide me some help regarding this.

Thanks in advance.

Hello, I am still a bit in the dark about unit test. Could you explain how tests are added and run. Also why TEST and TESTWITHSETUP these two macros are defined? What is the difference between these two macros.


#define TEST(name,classUnderTest)\
class classUnderTest##name##Test : public Test\
{ \
public: \
classUnderTest##name##Test () : Test (#name "Test") {} \
void setup() {}; \
void teardown() {}; \
void runTest (TestResult& result_); \
} classUnderTest##name##Instance; \
void classUnderTest##name##Test::runTest (TestResult& result_) \


#define TESTWITHSETUP(name,classUnderTest)\
class classUnderTest##name##Test : public Test, name##Setup\
{ \
public: \
classUnderTest##name##Test () : Test (#name "Test") {} \
void setup() {name##Setup::setup();} \
void teardown() {name##Setup::teardown();} \
void runTest (TestResult& result_); \
} classUnderTest##name##Instance; \
void classUnderTest##name##Test::runTest (TestResult& result_) \

Also at the last line of the two macros why only runTest function has been defined? Please explain a bit more.

Thanks in advance.

This topic is closed to new replies.

Advertisement