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

Addd test-fixtures docs from wiki

Commit
c22cfc4a95273f6f8314eb2150bdc65078dd2813
Author
Phil Nash ·
Parents
d60b24840938

Files · Download .tar.gz

Changes

 docs/reference-index.md |  1 +
 docs/test-fixtures.md   | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+)

diff --git a/docs/reference-index.md b/docs/reference-index.md
index 1b85cfb4..01d2799a 100644
--- a/docs/reference-index.md
+++ b/docs/reference-index.md
@@ -6,6 +6,7 @@ Before looking at this material be sure to read the [tutorial](tutorial.md)
 * [Assertion Macros](assertions.md)
 * [Logging Macros](logging.md)
 * [Supplying your own main()](own-main.md)
+* [Test fixtures](test-fixtures.md)
 
 ---
 
diff --git a/docs/test-fixtures.md b/docs/test-fixtures.md
new file mode 100644
index 00000000..769f8a37
--- /dev/null
+++ b/docs/test-fixtures.md
@@ -0,0 +1,32 @@
+Although Catch allows you to group tests together as sections within a test case, it can still convenient, sometimes, to group them using a more traditional test fixture. Catch fully supports this too. You define the test fixture as a simple structure:
+
+```c++
+class UniqueTestsFixture {
+  private:
+   static int uniqueID;
+  protected:
+   DBConnection conn;
+  public:
+   UniqueTestsFixture() : conn(DBConnection::createConnection("myDB")) {
+   }
+  protected:
+   int getID() {
+     return ++uniqueID;
+   }
+ };
+
+ int UniqueTestsFixture::uniqueID = 0;
+
+ TEST_CASE_METHOD(UniqueTestsFixture, "Create Employee/No Name", "[create]") {
+   REQUIRE_THROWS(conn.executeSQL("INSERT INTO employee (id, name) VALUES (?, ?)", getID(), ""));
+ }
+ TEST_CASE_METHOD(UniqueTestsFixture, "Create Employee/Normal", "[create]") {
+   REQUIRE(conn.executeSQL("INSERT INTO employee (id, name) VALUES (?, ?)", getID(), "Joe Bloggs"));
+ }
+```
+
+The two test cases here will create uniquely-named derived classes of UniqueTestsFixture and thus can access the `getID()` protected method and `conn` member variables. This ensures that both the test cases are able to create a DBConnection using the same method (DRY principle) and that any ID's created are unique such that the order that tests are executed does not matter.
+
+---
+
+[Home](../README.md)
\ No newline at end of file