aboutsummaryrefslogtreecommitdiff
path: root/unittest/Catch/docs/test-fixtures.md
diff options
context:
space:
mode:
authorSzczepan Zalega <szczepan@nitrokey.com>2017-12-22 16:53:56 +0100
committerSzczepan Zalega <szczepan@nitrokey.com>2017-12-22 16:53:56 +0100
commite9fc58cb304323f07aba736fc523903481404cff (patch)
tree59db450412ca101aa9601b3f2174436898f955c1 /unittest/Catch/docs/test-fixtures.md
parent8150ee4edc7e32d5c27cd3e0f68c630d90865638 (diff)
parent48b3d82ffe1ed19db9ba3cf7e6536ecf92e27391 (diff)
downloadlibnitrokey-e9fc58cb304323f07aba736fc523903481404cff.tar.gz
libnitrokey-e9fc58cb304323f07aba736fc523903481404cff.tar.bz2
Merge commit '48b3d82ffe1ed19db9ba3cf7e6536ecf92e27391' as 'unittest/Catch'
Diffstat (limited to 'unittest/Catch/docs/test-fixtures.md')
-rw-r--r--unittest/Catch/docs/test-fixtures.md32
1 files changed, 32 insertions, 0 deletions
diff --git a/unittest/Catch/docs/test-fixtures.md b/unittest/Catch/docs/test-fixtures.md
new file mode 100644
index 0000000..6bef762
--- /dev/null
+++ b/unittest/Catch/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