Jon Test
C++ Unit Test Tool. Not for production -- sample code only
Loading...
Searching...
No Matches
TestCase.h
Go to the documentation of this file.
1#ifndef JON_TEST_TEST_CASE_H
2#define JON_TEST_TEST_CASE_H
3
4#include "JonTest/Logger.h"
6
7#include <string>
8
9namespace JonTest
10{
11
16template <typename TestSuiteDerived>
18{
19public:
21 typedef TestSuiteDerived TestSuite;
22
24 typedef void (TestSuite::*TestCaseMethod)();
25
27 std::string name;
28
31
35 TestSuite& suite,
36 const char* const testCaseName,
38 )
39 {
40 suite.add(testCaseName, testCaseMethod);
41 return TestCase(testCaseName, testCaseMethod);
42 }
43
46 TestCase() = default;
47
50 TestCase(const TestCase& right) = default;
51
55 const char* const testCaseName,
57 )
58 : name(testCaseName),
60 {
61 // Nothing to do
62 }
63
67 {
68 // Nothing to do. DO NOT DELETE pointer-to-member testCaseMethod.
69 }
70
73 bool
75 TestSuite& suite,
76 Logger& logger
77 ) const
78 {
79 logger.startCase(suite.getName(), name);
80
81 if(nullptr == testCaseMethod)
82 {
83 logger.errorCase(suite.getName(), name, "(internal)", "Internal Error: MISSING test case method");
84 return false;
85 }
86
87 // Note: We use compressed/multiple-statements-per-line for clarity here
88 bool setupPassed = false;
89 try { suite.setup(); setupPassed = true; }
90 catch(TestFailure& failure) { logger.failCase(suite.getName(), name, "setup", failure); }
91 catch(std::exception& unexpected) { logger.exceptionCase(suite.getName(), name, "setup", unexpected); }
92 catch(...) { logger.errorCase(suite.getName(), name, "setup", "UNKNOWN EXCEPTION"); }
93
94 bool casePassed = false;
95 if(setupPassed) {
96 try { (suite.* testCaseMethod)(); casePassed = true; }
97 catch(TestFailure& failure) { logger.failCase(suite.getName(), name, "case", failure); }
98 catch(std::exception& unexpected) { logger.exceptionCase(suite.getName(), name, "case", unexpected); }
99 catch(...) { logger.errorCase(suite.getName(), name, "case", "UNKNOWN EXCEPTION"); }
100 }
101
102 bool teardownPassed = false;
103 try { suite.teardown(); teardownPassed = true; }
104 catch(TestFailure& failure) { logger.failCase(suite.getName(), name, "teardown", failure); }
105 catch(std::exception& unexpected) { logger.exceptionCase(suite.getName(), name, "teardown", unexpected); }
106 catch(...) { logger.errorCase(suite.getName(), name, "teardown", "UNKNOWN EXCEPTION"); }
107
108 const bool passed = setupPassed && casePassed && teardownPassed;
109 logger.endCase(suite.getName(), name, passed);
110 return passed;
111 }
112};
113
114}
115
116#endif
Interface for logging events while running tests.
Definition Logger.h:18
virtual void exceptionCase(const std::string &suiteName, const std::string &caseName, const std::string &casePart, const std::exception &unexpected)=0
Unexpected exception occurred during a part of the case.
virtual void startCase(const std::string &suiteName, const std::string &caseName)=0
Start of test case.
virtual void failCase(const std::string &suiteName, const std::string &caseName, const std::string &casePart, const TestFailure &failure)=0
Test failure occurred during a part of the case.
virtual void endCase(const std::string &suiteName, const std::string &caseName, const bool pass)=0
End of test case, with record pass or fail.
virtual void errorCase(const std::string &suiteName, const std::string &caseName, const std::string &casePart, const std::string &message)=0
Unexpected error occurred during a part of the test case.
Defines and runs a single Test Case within a Test Suite.
Definition TestCase.h:18
TestCase(const TestCase &right)=default
Default copy constructor for use in STL containers.
std::string name
Name of the Test Case.
Definition TestCase.h:27
~TestCase()
Do nothing destructor.
Definition TestCase.h:66
TestCaseMethod testCaseMethod
TestSuite method of the Test Case.
Definition TestCase.h:30
TestCase(const char *const testCaseName, TestCaseMethod const testCaseMethod)
Create Test Case by name for given testCaseMethod in TestSuite.
Definition TestCase.h:54
TestCase()=default
Default initializer for use in STL containers.
void(TestSuite::* TestCaseMethod)()
The actual Test Suite Method type for test cases.
Definition TestCase.h:24
TestSuiteDerived TestSuite
The actual Test Suite class.
Definition TestCase.h:21
bool run(TestSuite &suite, Logger &logger) const
Run this Test Case as part of suite, logging events to logger.
Definition TestCase.h:74
static TestCase createAndAdd(TestSuite &suite, const char *const testCaseName, TestCaseMethod const testCaseMethod)
Creates TestCase and adds the Test Case to the suite.
Definition TestCase.h:34
Exception thrown when a test fails an assert and should fail (unless wrapped in expectedFailure()).
Definition TestFailure.h:17
Definition Count.h:5