Wrap all uses in min and max in extra parentheses
[catch.git] / docs / test-fixtures.md
blobfc546b3997daa04e92efc507f7dd6be80505075d
1 Although Catch allows you to group tests together as sections within a test case, it can still be 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:
3 ```c++
4 class UniqueTestsFixture {
5   private:
6    static int uniqueID;
7   protected:
8    DBConnection conn;
9   public:
10    UniqueTestsFixture() : conn(DBConnection::createConnection("myDB")) {
11    }
12   protected:
13    int getID() {
14      return ++uniqueID;
15    }
16  };
18  int UniqueTestsFixture::uniqueID = 0;
20  TEST_CASE_METHOD(UniqueTestsFixture, "Create Employee/No Name", "[create]") {
21    REQUIRE_THROWS(conn.executeSQL("INSERT INTO employee (id, name) VALUES (?, ?)", getID(), ""));
22  }
23  TEST_CASE_METHOD(UniqueTestsFixture, "Create Employee/Normal", "[create]") {
24    REQUIRE(conn.executeSQL("INSERT INTO employee (id, name) VALUES (?, ?)", getID(), "Joe Bloggs"));
25  }
26 ```
28 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.
30 ---
32 [Home](Readme.md)