allow to select long tests to be run from cmdline

This commit is contained in:
Brice Gros 2019-10-18 15:09:38 +02:00
parent d658cc25e7
commit 22c3ae8d30
13 changed files with 115 additions and 43 deletions

View File

@ -200,7 +200,6 @@ option(POCO_UNBUNDLED
"Set to OFF|ON (default is OFF) to control linking dependencies as external" OFF)
if(ENABLE_TESTS)
option(ENABLE_LONG_RUNNING_TESTS "Enable long running test" ON)
include(CTest)
enable_testing()
message(STATUS "Building with unittests & samples")

View File

@ -25,11 +25,19 @@ class TestResult;
*/
class CppUnit_API Test
{
public:
enum Type {
Suite, // Only set on CppUnit::TestSuite
Normal, // Default TestCase always run
Long // Such TestCase will only be run if the `-long` command line argument is set
};
public:
virtual ~Test() = 0;
virtual void run(TestResult* result) = 0;
virtual int countTestCases() = 0;
virtual std::string toString() = 0;
virtual int countTestCases() const = 0;
virtual std::string toString() const = 0;
virtual Test::Type getType() const = 0;
void addSetup(const std::vector<std::string>& setup);
const std::vector<std::string>& setup() const;
@ -51,19 +59,26 @@ inline void Test::run(TestResult *result)
// Counts the number of test cases that will be run by this test.
inline int Test::countTestCases()
inline int Test::countTestCases() const
{
return 0;
}
// Returns the name of the test instance.
inline std::string Test::toString()
inline std::string Test::toString() const
{
return "";
}
// Returns the type of the test, see Test::Type
inline Test::Type Test::getType() const
{
return Test::Normal;
}
inline void Test::addSetup(const std::vector<std::string>& setup)
{
_setup = setup;

View File

@ -54,8 +54,8 @@ class TestCaller: public TestCase
typedef void (Fixture::*TestMethod)();
public:
TestCaller(const std::string& name, TestMethod test):
TestCase(name),
TestCaller(const std::string& name, TestMethod test, Test::Type testType = Test::Type::Normal):
TestCase(name, testType),
_test(test),
_fixture(new Fixture(name))
{
@ -95,7 +95,13 @@ private:
#define CppUnit_addTest(suite, cls, mth) \
suite->addTest(new CppUnit::TestCaller<cls>(#mth, &cls::mth))
#define CppUnit_addLongTest(suite, cls, mth) \
suite->addTest(new CppUnit::TestCaller<cls>(#mth, &cls::mth, CppUnit::Test::Long))
#define CppUnit_addQualifiedTest(suite, cls, mth) \
suite->addTest(new CppUnit::TestCaller<cls>(#cls"::"#mth, &cls::mth))
#define CppUnit_addLongQualifiedTest(suite, cls, mth) \
suite->addTest(new CppUnit::TestCaller<cls>(#cls"::"#mth, &cls::mth, CppUnit::Test::Long))
#endif // CppUnit_TestCaller_INCLUDED

View File

@ -87,14 +87,16 @@ class CppUnit_API TestCase: public Test
REFERENCEOBJECT (TestCase)
public:
TestCase(const std::string& Name);
TestCase(const std::string& Name, Test::Type testType = Test::Normal);
~TestCase();
virtual void run(TestResult* result);
virtual TestResult* run();
virtual int countTestCases();
virtual int countTestCases() const;
virtual std::string toString() const;
virtual Test::Type getType() const;
void setType(Test::Type testType);
const std::string& name() const;
std::string toString();
virtual void setUp();
virtual void setUp(const std::vector<std::string>& setup);
@ -169,12 +171,15 @@ protected:
private:
const std::string _name;
Test::Type _type;
};
// Constructs a test case
inline TestCase::TestCase(const std::string& name): _name (name)
inline TestCase::TestCase(const std::string& name, Test::Type testType)
: _name (name)
{
setType(testType);
}
@ -185,7 +190,7 @@ inline TestCase::~TestCase()
// Returns a count of all the tests executed
inline int TestCase::countTestCases()
inline int TestCase::countTestCases() const
{
return 1;
}
@ -217,12 +222,23 @@ inline void TestCase::tearDown()
// Returns the name of the test case instance
inline std::string TestCase::toString()
inline std::string TestCase::toString() const
{
const std::type_info& thisClass = typeid(*this);
return std::string(thisClass.name()) + "." + name();
}
// Returns the type of the test, see Test::Type
inline Test::Type TestCase::getType() const
{
return _type;
}
// Set the type of the test, see Test::Type
inline void TestCase::setType(Test::Type testType)
{
_type = testType;
}
// A set of macros which allow us to get the line number
// and file name at the point of an error.

View File

@ -33,7 +33,7 @@ class Test;
*
* Here is the synopsis:
*
* TestRunner [-all] [-print] [-wait] ExampleTestCase
* TestRunner [-all] [-long] [-print] [-wait] ExampleTestCase
*
*/
class CppUnit_API TestRunner
@ -54,6 +54,7 @@ protected:
void printBanner();
void print(const std::string& name, Test* pTest, int indent);
Test* find(const std::string& name, Test* pTest, const std::string& testName);
int collectAllTestCases(Test* pTest, std::vector<Test*>& tests);
private:
std::ostream& _ostr;

View File

@ -42,9 +42,10 @@ public:
~TestSuite();
void run(TestResult* result);
int countTestCases();
int countTestCases() const;
void addTest(Test* test);
std::string toString();
std::string toString() const;
Test::Type getType() const;
virtual void deleteContents();
@ -77,11 +78,16 @@ inline void TestSuite::addTest(Test* test)
// Returns a std::string representation of the test suite.
inline std::string TestSuite::toString()
inline std::string TestSuite::toString() const
{
return "suite " + _name;
}
// Returns the type of the test, see Test::Type
inline Test::Type TestSuite::getType() const
{
return Test::Suite;
}
// Returns all tests
inline const std::vector<Test*> TestSuite::tests() const

View File

@ -35,7 +35,7 @@ TestRunner::~TestRunner()
void TestRunner::printBanner()
{
_ostr
<< "Usage: driver [-all] [-print] [-wait] [name] ..." << std::endl
<< "Usage: driver [-all] [-long] [-print] [-wait] [name] ..." << std::endl
<< " where name is the name of a test case class" << std::endl;
}
@ -48,8 +48,11 @@ bool TestRunner::run(const std::vector<std::string>& args)
bool all = false;
bool wait = false;
bool printed = false;
bool long_running = false;
std::vector<std::string> setup;
std::vector<Test*> tests;
for (int i = 1; i < args.size(); i++)
{
const std::string& arg = args[i];
@ -63,6 +66,11 @@ bool TestRunner::run(const std::vector<std::string>& args)
all = true;
continue;
}
else if (arg == "-long")
{
long_running = true;
continue;
}
else if (arg == "-print")
{
for (Mappings::iterator it = _mappings.begin(); it != _mappings.end(); ++it)
@ -96,11 +104,8 @@ bool TestRunner::run(const std::vector<std::string>& args)
}
if (testToRun)
{
if (setup.size() > 0)
testToRun->addSetup(setup);
if (!run(testToRun)) success = false;
collectAllTestCases(testToRun, tests);
}
numberOfTests++;
if (!testToRun)
{
@ -112,15 +117,24 @@ bool TestRunner::run(const std::vector<std::string>& args)
if (all)
{
tests.clear();
for (Mappings::iterator it = _mappings.begin(); it != _mappings.end(); ++it)
{
if (setup.size() > 0)
it->second->addSetup(setup);
if (!run(it->second)) success = false;
numberOfTests++;
collectAllTestCases(it->second, tests);
}
}
for (std::vector<Test*>::const_iterator it = tests.begin(); it != tests.end(); ++it)
{
Test* testToRun = *it;
if(testToRun->getType() == Test::Long && !long_running)
continue;
if (setup.size() > 0)
testToRun->addSetup(setup);
if (!run(testToRun)) success = false;
numberOfTests++;
}
if (numberOfTests == 0 && !printed)
{
printBanner();
@ -194,4 +208,29 @@ Test* TestRunner::find(const std::string& name, Test* pTest, const std::string&
}
int TestRunner::collectAllTestCases(Test* pTest, std::vector<Test*>& testcases)
{
int added = 0;
if (pTest->getType() == Test::Suite)
{
TestSuite* pSuite = dynamic_cast<TestSuite*>(pTest);
if (pSuite)
{
const std::vector<Test*>& tests = pSuite->tests();
for (std::vector<Test*>::const_iterator it = tests.begin(); it != tests.end(); ++it)
{
added += collectAllTestCases(*it, testcases);
}
}
}
else
{
testcases.push_back(pTest);
added = 1;
}
return added;
}
} // namespace CppUnit

View File

@ -35,11 +35,11 @@ void TestSuite::run(TestResult *result)
// Counts the number of test cases that will be run by this test.
int TestSuite::countTestCases()
int TestSuite::countTestCases() const
{
int count = 0;
for (std::vector<Test*>::iterator it = _tests.begin (); it != _tests.end (); ++it)
for (std::vector<Test*>::const_iterator it = _tests.begin (); it != _tests.end (); ++it)
count += (*it)->countTestCases();
return count;

View File

@ -1,2 +1,2 @@
add_executable(StringTokenizer src/StringTokenizer.cpp)
target_link_libraries(StringTokenizer PUBLIC Poco::JSON Poco::XML Poco::Foundation)
target_link_libraries(StringTokenizer PUBLIC Poco::Foundation)

View File

@ -1,2 +1,2 @@
add_executable(URI src/URI.cpp)
target_link_libraries(URI PUBLIC Poco::JSON Poco::XML Poco::Foundation )
target_link_libraries(URI PUBLIC Poco::Foundation )

View File

@ -41,10 +41,6 @@ if(UNIX AND NOT ANDROID)
target_link_libraries(Foundation-testrunner PUBLIC pthread)
endif(UNIX AND NOT ANDROID)
if(ENABLE_LONG_RUNNING_TESTS)
target_compile_definitions(Foundation-testrunner PRIVATE ENABLE_LONG_RUNNING_TESTS)
endif(ENABLE_LONG_RUNNING_TESTS)
# TestApp
if(WINCE)
add_executable( TestApp src/TestApp_WINCE.cpp )

View File

@ -74,14 +74,12 @@ void PBKDF2EngineTest::testPBKDF2c()
void PBKDF2EngineTest::testPBKDF2d()
{
// test vector 4 from RFC 6070
#if defined(ENABLE_LONG_RUNNING_TESTS)
std::string p("password");
std::string s("salt");
PBKDF2Engine<HMACEngine<SHA1Engine> > pbkdf2(s, 16777216, 20);
pbkdf2.update(p);
std::string dk = DigestEngine::digestToHex(pbkdf2.digest());
assertTrue (dk == "eefe3d61cd4da4e4e9945b3d6ba2158c2634e984");
#endif // defined(ENABLE_LONG_RUNNING_TESTS)
}
@ -128,7 +126,7 @@ CppUnit::Test* PBKDF2EngineTest::suite()
CppUnit_addTest(pSuite, PBKDF2EngineTest, testPBKDF2a);
CppUnit_addTest(pSuite, PBKDF2EngineTest, testPBKDF2b);
CppUnit_addTest(pSuite, PBKDF2EngineTest, testPBKDF2c);
CppUnit_addTest(pSuite, PBKDF2EngineTest, testPBKDF2d);
CppUnit_addLongTest(pSuite, PBKDF2EngineTest, testPBKDF2d);
CppUnit_addTest(pSuite, PBKDF2EngineTest, testPBKDF2e);
CppUnit_addTest(pSuite, PBKDF2EngineTest, testPBKDF2f);

View File

@ -131,7 +131,6 @@ RWLockTest::~RWLockTest()
void RWLockTest::testLock()
{
#if defined(ENABLE_LONG_RUNNING_TESTS)
RWLock lock;
int counter = 0;
RWLockRunnable r1(lock, counter);
@ -160,13 +159,11 @@ void RWLockTest::testLock()
assertTrue (r3.ok());
assertTrue (r4.ok());
assertTrue (r5.ok());
#endif // defined(ENABLE_LONG_RUNNING_TESTS)
}
void RWLockTest::testTryLock()
{
#if defined(ENABLE_LONG_RUNNING_TESTS)
RWLock lock;
int counter = 0;
RWTryLockRunnable r1(lock, counter);
@ -195,7 +192,6 @@ void RWLockTest::testTryLock()
assertTrue (r3.ok());
assertTrue (r4.ok());
assertTrue (r5.ok());
#endif // defined(ENABLE_LONG_RUNNING_TESTS)
}
@ -213,8 +209,8 @@ CppUnit::Test* RWLockTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("RWLockTest");
CppUnit_addTest(pSuite, RWLockTest, testLock);
CppUnit_addTest(pSuite, RWLockTest, testTryLock);
CppUnit_addLongTest(pSuite, RWLockTest, testLock);
CppUnit_addLongTest(pSuite, RWLockTest, testTryLock);
return pSuite;
}