1

I have the following code in file tested.cpp:

#include <iostream>
using namespace std;

class tested {
    private:
        int x;
    public:
        tested(int x_inp) {
            x = x_inp;
        }

        int getValue() {
            return x;
        }
};

I also have another file (called testing.cpp):

#include <cppunit/extensions/HelperMacros.h>
#include "tested.cpp"

class TestTested : public CppUnit::TestFixture
{
    CPPUNIT_TEST_SUITE(TestTested);
    CPPUNIT_TEST(check_value);
    CPPUNIT_TEST_SUITE_END();

    public:
        void check_value();
};

CPPUNIT_TEST_SUITE_REGISTRATION(TestTested);

void TestTested::check_value() {
    tested t(3);
    int expected_val = t.getValue();
    CPPUNIT_ASSERT_EQUAL(7, expected_val);
}

When I try to compile the testing.cpp file I get: undefined reference tomain'`. Well, this is because I do not have main (the entry point for the program). So, the compiler does not know how to start the execution of the code.

But what is not clear to me is how to execute the code in the testing.cpp. I tried to add:

int main() {
        TestTested t();
        return 1;
}

However, it does not print anything (and it is expected to return an error message since 3 is not equal to 7).

Does anybody know what is the correct way to run the unit test?

Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
Roman
  • 124,451
  • 167
  • 349
  • 456
  • 1. You shouldn't include "*.cpp" file and 2. Does creating an object call the check_value method? There may be other method to be called to start tests... – Caladan Apr 18 '13 at 08:30
  • 2
    you shouldnt include cpp files, rather h/lib files. anyhow the first line in main is declaring a function – Ariel Pinchover Apr 18 '13 at 08:30
  • But I have only one cpp file. If I do not include it, my `testing.cpp` will not know what is declared in `tested.cpp`. – Roman Apr 18 '13 at 08:32
  • 2
    @Roman The second part of Infested's remark is really important: You need to declare `t` as `TestTested t;`, not `TestTested t();`. I.e., remove the round brackets. That's important. It changes the meaning of the declaration completely. – jogojapan Apr 18 '13 at 08:35
  • http://stackoverflow.com/questions/3810570/why-is-there-no-call-to-the-constructor – jogojapan Apr 18 '13 at 08:36
  • @jogojapan not necessarily, you can also (theoretically, if the constructor can get void) write TestTested (t()); – Ariel Pinchover Apr 18 '13 at 08:39
  • @Infested Not necessarily what? I.e., what is not necessarily the case? – jogojapan Apr 18 '13 at 08:44
  • @jogojapan Not necessarily "remove the round brackets" – Ariel Pinchover Apr 18 '13 at 11:13
  • @Infested, `TestTested (t());` is also a function declaration. – M.M May 01 '14 at 04:34

1 Answers1

2

Since you are writing a cppunit test, why not looking at cppunit doc ? (http://cppunit.sourceforge.net/doc/lastest/cppunit_cookbook.html)

It tells you that the main sould be written like this :

#include <cppunit/ui/text/TestRunner.h>
#include "ExampleTestCase.h"
#include "ComplexNumberTest.h"

int main( int argc, char **argv) {
  CppUnit::TextUi::TestRunner runner;
  runner.addTest( ExampleTestCase::suite() );
  runner.addTest( ComplexNumberTest::suite() );
  runner.run();
  return 0;
} 
Offirmo
  • 18,962
  • 12
  • 76
  • 97