[英]Where to put main and what to write there?
我在文件tested.cpp
有以下代碼:
#include <iostream>
using namespace std;
class tested {
private:
int x;
public:
tested(int x_inp) {
x = x_inp;
}
int getValue() {
return x;
}
};
我還有另一個文件(稱為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);
}
當我嘗試編譯testing.cpp
文件時,我得到: undefined reference to
main'`的undefined reference to
。 好吧,這是因為我沒有main(程序的入口點)。 因此,編譯器不知道如何開始執行代碼。
但是對我來說不清楚的是如何在testing.cpp
執行代碼。 我嘗試添加:
int main() {
TestTested t();
return 1;
}
但是,它不打印任何內容(由於3不等於7,因此它會返回一條錯誤消息)。
有人知道運行單元測試的正確方法是什么嗎?
由於您正在編寫cppunit測試,所以為什么不查看cppunit doc呢? ( http://cppunit.sourceforge.net/doc/lastest/cppunit_cookbook.html )
它告訴你主要的靈魂是這樣寫的:
#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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.