test_cases_and_suites.cpp (2226B)
1 #include <doctest/doctest.h> 2 3 #include "header.h" 4 5 static int doStuff() { 6 int a = 5; 7 a += 2; 8 // asserts and other doctest functionality can be used in user code if checked for a testing context 9 // AND they can also be used without such checks - see "asserts_used_outside_of_tests.cpp" 10 if(doctest::is_running_in_test) 11 CHECK(a == 7); 12 13 return a; 14 } 15 16 TEST_CASE("an empty test that will succeed - not part of a test suite") {} 17 18 TEST_CASE("should fail because of an exception") { 19 doStuff(); 20 21 throw_if(true, 0); 22 } 23 24 TEST_SUITE("scoped test suite") { 25 TEST_CASE("part of scoped") { 26 FAIL(""); 27 } 28 29 TEST_CASE("part of scoped 2") { 30 FAIL(""); 31 } 32 } 33 34 TEST_SUITE_BEGIN("some TS"); // begin "some TS" 35 36 TEST_CASE("part of some TS") { 37 FAIL(""); 38 } 39 40 TEST_SUITE_END(); // ends "some TS" 41 42 TEST_CASE_FIXTURE(SomeFixture, "fixtured test - not part of a test suite") { 43 data /= 2; 44 CHECK(data == 85); 45 } 46 47 TEST_CASE("normal test in a test suite from a decorator" * doctest::test_suite("ts1") * 48 doctest::timeout(0.000001)) { 49 MESSAGE("failing because of the timeout decorator!"); 50 } 51 52 static bool shouldSkip() { return false; } 53 54 TEST_SUITE("skipped test cases" * doctest::skip()) { 55 TEST_CASE("unskipped" * doctest::skip(shouldSkip()) * 56 doctest::description("this test has overridden its skip decorator")) { 57 FAIL(""); 58 } 59 TEST_CASE("skipped - inherited from the test suite") { FAIL(""); } 60 } 61 62 TEST_SUITE("test suite with a description" * doctest::description("regarding failures")) { 63 TEST_CASE("fails - and its allowed" * doctest::may_fail()) { FAIL(""); } 64 TEST_CASE("doesn't fail which is fine" * doctest::may_fail()) {} 65 66 TEST_CASE("fails as it should" * doctest::should_fail()) { FAIL(""); } 67 TEST_CASE("doesn't fail but it should have" * doctest::should_fail()) {} 68 69 TEST_CASE("fails 1 time as it should" * doctest::expected_failures(1)) { FAIL(""); } 70 TEST_CASE("fails more times than it should" * doctest::expected_failures(1)) { 71 FAIL_CHECK(""); 72 FAIL_CHECK(""); 73 } 74 } 75 76 TEST_CASE("should fail and no output" * doctest::should_fail() * doctest::no_breaks() * doctest::no_output()) { FAIL(""); }