summaryrefslogtreecommitdiff
path: root/include/cppunit
diff options
context:
space:
mode:
authorMarkus Mohrhard <markus.mohrhard@googlemail.com>2016-12-16 13:53:51 +0100
committerMarkus Mohrhard <markus.mohrhard@googlemail.com>2016-12-16 14:05:07 +0100
commit4e529c6a6569d1f352e02af16e53aba7ae7bdc1a (patch)
treebbbb7cfe6d872406cfa38381f91e6411ab9d6822 /include/cppunit
parent923e2a837d515eb0d33792aba8bbb839f0012067 (diff)
downloadcppunit-4e529c6a6569d1f352e02af16e53aba7ae7bdc1a.tar.gz
implement parameterized tests
This allows to execute the same test with different parameters and treats each execution as an own test. The change consists of two parts, the TestCaller can now handle any callable which also makes it easy to generate programatically more complex test cases as well as the new CPPUNIT_TEST_PARAMETERIZED macro. That macro takes the test name as well as an iteratable, e.g. std::initializer_list. An example for this usage is: class SimpleTest : public CppUnit::TestFixture { public: CPPUNIT_TEST_SUITE(SimpleTest); CPPUNIT_TEST_PARAMETERIZED(test, {1, 2, 3, 4}); CPPUNIT_TEST_SUITE_END(); void test(int i) { CPPUNIT_ASSERT(i < 5); } }; which will execute test 4 times with the values 1 to 4.
Diffstat (limited to 'include/cppunit')
-rw-r--r--include/cppunit/extensions/HelperMacros.h11
-rw-r--r--include/cppunit/extensions/TestNamer.h7
-rw-r--r--include/cppunit/extensions/TestSuiteBuilderContext.h15
3 files changed, 33 insertions, 0 deletions
diff --git a/include/cppunit/extensions/HelperMacros.h b/include/cppunit/extensions/HelperMacros.h
index 3dfab2d..4c30319 100644
--- a/include/cppunit/extensions/HelperMacros.h
+++ b/include/cppunit/extensions/HelperMacros.h
@@ -301,6 +301,17 @@ public: \
&TestFixtureType::testMethod, \
context.makeFixture() ) ) )
+#define CPPUNIT_TEST_PARAMETERIZED( testMethod, ... ) \
+ for (auto& i : __VA_ARGS__) \
+ { \
+ TestFixtureType* fixture = context.makeFixture(); \
+ CPPUNIT_TEST_SUITE_ADD_TEST( \
+ ( new CPPUNIT_NS::TestCaller<TestFixtureType>( \
+ context.getTestNameFor(#testMethod, i), \
+ std::bind(&TestFixtureType::testMethod, fixture, i), \
+ fixture))); \
+ }
+
/*! \brief Add a test which fail if the specified exception is not caught.
*
* Example:
diff --git a/include/cppunit/extensions/TestNamer.h b/include/cppunit/extensions/TestNamer.h
index 3907569..0c8fb31 100644
--- a/include/cppunit/extensions/TestNamer.h
+++ b/include/cppunit/extensions/TestNamer.h
@@ -3,6 +3,7 @@
#include <cppunit/Portability.h>
#include <string>
+#include <cppunit/tools/StringHelper.h>
#include <typeinfo>
@@ -63,6 +64,12 @@ public:
*/
virtual std::string getTestNameFor( const std::string &testMethodName ) const;
+ template<typename E>
+ std::string getTestNameFor( const std::string& testMethodName, const E& val) const
+ {
+ return getTestNameFor(testMethodName) + " with parameter: " + CPPUNIT_NS::StringHelper::toString(val);
+ }
+
protected:
std::string m_fixtureName;
};
diff --git a/include/cppunit/extensions/TestSuiteBuilderContext.h b/include/cppunit/extensions/TestSuiteBuilderContext.h
index b62eeb8..72bfa70 100644
--- a/include/cppunit/extensions/TestSuiteBuilderContext.h
+++ b/include/cppunit/extensions/TestSuiteBuilderContext.h
@@ -62,6 +62,21 @@ public:
*/
std::string getTestNameFor( const std::string &testMethodName ) const;
+ /*! \brief Returns the name of the test for the specified method with the corresponding parameter.
+ *
+ * \param testMethodName Name (including a parameter) of the method that implements a test.
+ * \return A string that is the concatenation of the test fixture name
+ * (returned by getFixtureName()), \a testMethodName,
+ * separated using '::' and the parameter. This provides a fairly unique name for a given
+ * test. The parameter must be convertable to std::string through operator<<
+ * or a specialization of CPPUNIT_NS::StringHelper::toString needs to exist.
+ */
+ template<typename T>
+ std::string getTestNameFor( const std::string &testMethodName, const T& value ) const
+ {
+ return m_namer.getTestNameFor(testMethodName, value);
+ }
+
/*! \brief Adds property pair.
* \param key PropertyKey string to add.
* \param value PropertyValue string to add.