Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

Added facility to write self-registering test cases as methods

Commit
a36b8d0779b708146a226ae81616c4cd5ffda126
Author
Phil Nash ·
Parents
7abc6365a917

Files · Download .tar.gz

Changes

 Test/ClassTests.cpp         | 20 ++++++++++++++++++++
 internal/catch_registry.hpp | 11 +++++++++++
 2 files changed, 31 insertions(+)

diff --git a/Test/ClassTests.cpp b/Test/ClassTests.cpp
index b205ed78..f37f7565 100644
--- a/Test/ClassTests.cpp
+++ b/Test/ClassTests.cpp
@@ -33,5 +33,25 @@ namespace
         }
     };
 }
+
+
 METHOD_AS_TEST_CASE( TestClass::succeedingCase, "succeeding/TestClass/succeedingCase", "A method based test run that succeeds" );
 METHOD_AS_TEST_CASE( TestClass::failingCase, "failing/TestClass/failingCase", "A method based test run that fails" );
+
+
+struct Fixture
+{
+    Fixture() : m_a( 1 ) {}
+    
+    int m_a;
+};
+
+TEST_CASE_METHOD( Fixture, "succeeding/Fixture/succeedingCase", "A method based test run that succeeds" )
+{   
+    REQUIRE( m_a == 1 );        
+}
+
+TEST_CASE_METHOD( Fixture, "succeeding/Fixture/failingCase", "A method based test run that fails" )
+{   
+    REQUIRE( m_a == 2 );        
+}
diff --git a/internal/catch_registry.hpp b/internal/catch_registry.hpp
index dcd0d13b..64e91322 100644
--- a/internal/catch_registry.hpp
+++ b/internal/catch_registry.hpp
@@ -134,6 +134,9 @@ struct AutoReg
         TestRegistry::instance().registerTest( TestCaseInfo( new MethodTestCase<C>( method ), name, description ) );
     }
 };
+
+template<typename T, size_t>
+struct FixtureWrapper{};
     
 } // end namespace Catch
 
@@ -145,4 +148,12 @@ struct AutoReg
 #define CATCH_METHOD_AS_TEST_CASE( QualifiedMethod, Name, Desc ) \
     namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &QualifiedMethod, Name, Desc ); }
 
+#define TEST_CASE_METHOD( ClassName, TestName, Desc )\
+    namespace Catch{ template<> struct FixtureWrapper<ClassName, __LINE__> : ClassName \
+    { \
+        void test(); \
+    }; }\
+    namespace { Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar ) ( &Catch::FixtureWrapper<ClassName, __LINE__>::test, TestName, Desc ); } \
+    void Catch::FixtureWrapper<ClassName, __LINE__>::test()
+
 #endif // TWOBLUECUBES_CATCH_REGISTRY_HPP_INCLUDED