Thoran hat geschrieben:Interessantes Thema, nachdem ich auch Unittests mit CMakes CTest machen wollte. Ich hatte mir mal das CppUnit-Framework installiert und die Doku ausgedruckt, bin aber noch nicht dazu gekommen sie zu lesen. Von daher, kaiserludi, wäre es schön wenn du deine Erfahrung teilen könntest. Z.b. Aufwand zum Schreiben eines Testtreibers. Aufwand der Einbindung in CTest.
Thoran
So sieht bei uns ein typischer Testcase aus:
- Code: Ansicht erweitern :: Alles auswählen
/**
* Test of <PhotonPeer::Connect>.
* +
*/
void PhotonUnitTest::testPhotonPeerConnect(void)
{
//! Check connection for correct IP address with explicit correct port (EG_PORT_xxx).
JString Url = PhotonUnitTest::getServerAddress(true);
bool connectResult = peer1->connect(Url);
CPPUNIT_ASSERT(connectResult);
JString Msg = L"Connect To Correct Server with explicit port (" + Url + ")";
checkStatusForPeer1(Msg, SC_CONNECT);
peer1->disconnect();
checkStatusForPeer1(L"Disconnect From Correct Server (with port)", SC_DISCONNECT);
//! Check connection for incorrect IP address.
Url = L"328.0.0.1";
Msg = L"Connect To Incorrect IP address (" + Url + ")";
connectResult = peer1->connect(Url);
CPPUNIT_ASSERT(!connectResult);
//! Check connection for correct, but not responding, IP
Url = L"google.com";
connectResult = peer1->connect(Url);
CPPUNIT_ASSERT(connectResult);
Msg = L"Connect To Incorrect Server (" + Url + ")";
checkStatusForPeer1(Msg, SC_TIMEOUT_DISCONNECT);
//! Check connection for correct IP address with incorrect MIN port (0000).
// port 0 is a special case: this port is interpreted as syntax error (for no number found, the lib is remaining on init-value, which is 0) in port-part of the string by PhotonPeerConnect and so function-call will return an error and we do not have to wait for a timeout like for other ports
JString serverIPCorrectPortIncorrectMinValue = PhotonUnitTest::getServerAddress(false) + ":0000";
Msg = L"Connect To Incorrect Server (" + serverIPCorrectPortIncorrectMinValue + ")";
connectResult = peer1->connect(serverIPCorrectPortIncorrectMinValue);
CPPUNIT_ASSERT(!connectResult);
//! Check connection for correct IP address with incorrect MAX port (65535).
JString serverIPCorrectPortIncorrectMaxValue = PhotonUnitTest::getServerAddress(false) + ":65535";
connectResult = peer1->connect(serverIPCorrectPortIncorrectMaxValue);
CPPUNIT_ASSERT(connectResult);
Msg = L"Connect To Incorrect Server (" + serverIPCorrectPortIncorrectMinValue + ")";
checkStatusForPeer1(L"Connect To Incorrect Server", SC_TIMEOUT_DISCONNECT);
}
Der wird dann so aufgerufen:
- Code: Ansicht erweitern :: Alles auswählen
suite->addTest(new CppUnit::TestCaller<PhotonUnitTest>("\n\n testPhotonPeerConnect", &PhotonUnitTest::testPhotonPeerConnect));
Das wars, ein neuer Testcase wurde hinzugefügt.
Keine Ahnung, was du mit Testtreiber meinst und wie aufwendig es ist, dass in CTest einzubinden, kann ich nicht sagen. Wir haben die Unittests als Visual Studio Projekt, welches die zu testenden Libs einbindet und eine executable erzeugt, aufgesetzt.