Skip to content

Commit 6b478c3

Browse files
rikardfalkebornamai2012
authored andcommitted
Allow multiple test case arguments to testrunner (danmar#1755)
Take some care to not run the same test case twice, even if running: ./testrunner TestClass TestClass::TestCase
1 parent c262aef commit 6b478c3

File tree

4 files changed

+48
-33
lines changed

4 files changed

+48
-33
lines changed

test/options.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,19 @@
1919
#include <iterator>
2020

2121
options::options(int argc, const char* const argv[])
22-
:mOptions(argv + 1, argv + argc)
23-
,mWhichTest("")
24-
,mQuiet(mOptions.count("-q") != 0)
25-
,mHelp(mOptions.count("-h") != 0 || mOptions.count("--help"))
22+
:mWhichTests(argv + 1, argv + argc)
23+
,mQuiet(mWhichTests.count("-q") != 0)
24+
,mHelp(mWhichTests.count("-h") != 0 || mWhichTests.count("--help"))
2625
{
27-
mOptions.erase("-q");
28-
mOptions.erase("-h");
29-
mOptions.erase("--help");
30-
if (! mOptions.empty()) {
31-
mWhichTest = *mOptions.rbegin();
26+
for (std::set<std::string>::const_iterator it = mWhichTests.begin(); it != mWhichTests.end();) {
27+
if (!(*it).empty() && (((*it)[0] == '-') || ((*it).find("::") != std::string::npos && mWhichTests.count((*it).substr(0, (*it).find("::"))))))
28+
it = mWhichTests.erase(it);
29+
else
30+
++it;
31+
}
32+
33+
if (mWhichTests.empty()) {
34+
mWhichTests.insert("");
3235
}
3336
}
3437

@@ -42,7 +45,7 @@ bool options::help() const
4245
return mHelp;
4346
}
4447

45-
const std::string& options::which_test() const
48+
const std::set<std::string>& options::which_test() const
4649
{
47-
return mWhichTest;
50+
return mWhichTests;
4851
}

test/options.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,15 @@ class options {
3434
/** Print help. */
3535
bool help() const;
3636
/** Which test should be run. Empty string means 'all tests' */
37-
const std::string& which_test() const;
37+
const std::set<std::string>& which_test() const;
3838

3939
private:
4040
options();
4141
options(const options& non_copy);
4242
const options& operator =(const options& non_assign);
4343

4444
private:
45-
std::set<std::string> mOptions;
46-
std::string mWhichTest;
45+
std::set<std::string> mWhichTests;
4746
const bool mQuiet;
4847
const bool mHelp;
4948
};

test/testoptions.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,29 @@ class TestOptions: public TestFixture {
3636
TEST_CASE(help);
3737
TEST_CASE(help_long);
3838
TEST_CASE(multiple_testcases);
39+
TEST_CASE(multiple_testcases_ignore_duplicates);
3940
TEST_CASE(invalid_switches);
4041
}
4142

4243

4344
void which_test() const {
4445
const char* argv[] = {"./test_runner", "TestClass"};
4546
options args(sizeof argv / sizeof argv[0], argv);
46-
ASSERT_EQUALS("TestClass", args.which_test());
47+
ASSERT(std::set<std::string> {"TestClass"} == args.which_test());
4748
}
4849

4950

5051
void which_test_method() const {
5152
const char* argv[] = {"./test_runner", "TestClass::TestMethod"};
5253
options args(sizeof argv / sizeof argv[0], argv);
53-
ASSERT_EQUALS("TestClass::TestMethod", args.which_test());
54+
ASSERT(std::set<std::string> {"TestClass::TestMethod"} == args.which_test());
5455
}
5556

5657

5758
void no_test_method() const {
5859
const char* argv[] = {"./test_runner"};
5960
options args(sizeof argv / sizeof argv[0], argv);
60-
ASSERT_EQUALS("", args.which_test());
61+
ASSERT(std::set<std::string> {""} == args.which_test());
6162
}
6263

6364

@@ -95,16 +96,24 @@ class TestOptions: public TestFixture {
9596
}
9697

9798
void multiple_testcases() const {
98-
const char* argv[] = {"./test_runner", "TestClass::TestMethod", "Ignore::ThisOne"};
99+
const char* argv[] = {"./test_runner", "TestClass::TestMethod", "TestClass::AnotherTestMethod"};
99100
options args(sizeof argv / sizeof argv[0], argv);
100-
ASSERT_EQUALS("TestClass::TestMethod", args.which_test());
101+
std::set<std::string> expected {"TestClass::TestMethod", "TestClass::AnotherTestMethod"};
102+
ASSERT(expected == args.which_test());
101103
}
102104

105+
void multiple_testcases_ignore_duplicates() const {
106+
const char* argv[] = {"./test_runner", "TestClass::TestMethod", "TestClass"};
107+
options args(sizeof argv / sizeof argv[0], argv);
108+
std::set<std::string> expected {"TestClass"};
109+
ASSERT(expected == args.which_test());
110+
}
103111

104112
void invalid_switches() const {
105113
const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-a", "-v", "-q"};
106114
options args(sizeof argv / sizeof argv[0], argv);
107-
ASSERT_EQUALS("TestClass::TestMethod", args.which_test());
115+
std::set<std::string> expected {"TestClass::TestMethod"};
116+
ASSERT(expected == args.which_test());
108117
ASSERT_EQUALS(true, args.quiet());
109118
}
110119
};

test/testsuite.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -280,13 +280,15 @@ void TestFixture::printHelp()
280280
std::cout << "Testrunner - run Cppcheck tests\n"
281281
"\n"
282282
"Syntax:\n"
283-
" testrunner [OPTIONS] [TestClass::TestCase]\n"
283+
" testrunner [OPTIONS] [TestClass::TestCase...]\n"
284284
" run all test cases:\n"
285285
" testrunner\n"
286286
" run all test cases in TestClass:\n"
287287
" testrunner TestClass\n"
288288
" run TestClass::TestCase:\n"
289289
" testrunner TestClass::TestCase\n"
290+
" run all test cases in TestClass1 and TestClass2::TestCase:\n"
291+
" testrunner TestClass1 TestClass2::TestCase\n"
290292
"\n"
291293
"Options:\n"
292294
" -q Do not print the test cases that have run.\n"
@@ -311,22 +313,24 @@ void TestFixture::processOptions(const options& args)
311313

312314
std::size_t TestFixture::runTests(const options& args)
313315
{
314-
std::string classname(args.which_test());
315-
std::string testname;
316-
if (classname.find("::") != std::string::npos) {
317-
testname = classname.substr(classname.find("::") + 2);
318-
classname.erase(classname.find("::"));
319-
}
320-
321316
countTests = 0;
322317
errmsg.str("");
323318

324-
const TestSet &tests = TestRegistry::theInstance().tests();
319+
const std::set<std::string>& tests = args.which_test();
320+
for (std::string classname : tests) {
321+
std::string testname;
322+
if (classname.find("::") != std::string::npos) {
323+
testname = classname.substr(classname.find("::") + 2);
324+
classname.erase(classname.find("::"));
325+
}
325326

326-
for (TestFixture * test : tests) {
327-
if (classname.empty() || test->classname == classname) {
328-
test->processOptions(args);
329-
test->run(testname);
327+
const TestSet &tests = TestRegistry::theInstance().tests();
328+
329+
for (TestFixture * test : tests) {
330+
if (classname.empty() || test->classname == classname) {
331+
test->processOptions(args);
332+
test->run(testname);
333+
}
330334
}
331335
}
332336

0 commit comments

Comments
 (0)