FIXTURES_REQUIRED.rst 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. FIXTURES_REQUIRED
  2. -----------------
  3. Specifies a list of fixtures the test requires. Fixture names are case
  4. sensitive and they are not required to have any similarity to test names.
  5. Fixtures are a way to attach setup and cleanup tasks to a set of tests. If a
  6. test requires a given fixture, then all tests marked as setup tasks for that
  7. fixture will be executed first (once for the whole set of tests, not once per
  8. test requiring the fixture). After all tests requiring a particular fixture
  9. have completed, CTest will ensure all tests marked as cleanup tasks for that
  10. fixture are then executed. Tests are marked as setup tasks with the
  11. :prop_test:`FIXTURES_SETUP` property and as cleanup tasks with the
  12. :prop_test:`FIXTURES_CLEANUP` property. If any of a fixture's setup tests fail,
  13. all tests listing that fixture in their ``FIXTURES_REQUIRED`` property will not
  14. be executed. The cleanup tests for the fixture will always be executed, even if
  15. some setup tests fail.
  16. When CTest is asked to execute only a subset of tests (e.g. by the use of
  17. regular expressions or when run with the ``--rerun-failed`` command line
  18. option), it will automatically add any setup or cleanup tests for fixtures
  19. required by any of the tests that are in the execution set. This behavior can
  20. be overridden with the ``-FS``, ``-FC`` and ``-FA`` command line options to
  21. :manual:`ctest(1)` if desired.
  22. Since setup and cleanup tasks are also tests, they can have an ordering
  23. specified by the :prop_test:`DEPENDS` test property just like any other tests.
  24. This can be exploited to implement setup or cleanup using multiple tests for a
  25. single fixture to modularise setup or cleanup logic.
  26. The concept of a fixture is different to that of a resource specified by
  27. :prop_test:`RESOURCE_LOCK`, but they may be used together. A fixture defines a
  28. set of tests which share setup and cleanup requirements, whereas a resource
  29. lock has the effect of ensuring a particular set of tests do not run in
  30. parallel. Some situations may need both, such as setting up a database,
  31. serialising test access to that database and deleting the database again at the
  32. end. For such cases, tests would populate both ``FIXTURES_REQUIRED`` and
  33. :prop_test:`RESOURCE_LOCK` to combine the two behaviours. Names used for
  34. :prop_test:`RESOURCE_LOCK` have no relationship with names of fixtures, so note
  35. that a resource lock does not imply a fixture and vice versa.
  36. Consider the following example which represents a database test scenario
  37. similar to that mentioned above:
  38. .. code-block:: cmake
  39. add_test(NAME testsDone COMMAND emailResults)
  40. add_test(NAME fooOnly COMMAND testFoo)
  41. add_test(NAME dbOnly COMMAND testDb)
  42. add_test(NAME dbWithFoo COMMAND testDbWithFoo)
  43. add_test(NAME createDB COMMAND initDB)
  44. add_test(NAME setupUsers COMMAND userCreation)
  45. add_test(NAME cleanupDB COMMAND deleteDB)
  46. add_test(NAME cleanupFoo COMMAND removeFoos)
  47. set_tests_properties(setupUsers PROPERTIES DEPENDS createDB)
  48. set_tests_properties(createDB PROPERTIES FIXTURES_SETUP DB)
  49. set_tests_properties(setupUsers PROPERTIES FIXTURES_SETUP DB)
  50. set_tests_properties(cleanupDB PROPERTIES FIXTURES_CLEANUP DB)
  51. set_tests_properties(cleanupFoo PROPERTIES FIXTURES_CLEANUP Foo)
  52. set_tests_properties(testsDone PROPERTIES FIXTURES_CLEANUP "DB;Foo")
  53. set_tests_properties(fooOnly PROPERTIES FIXTURES_REQUIRED Foo)
  54. set_tests_properties(dbOnly PROPERTIES FIXTURES_REQUIRED DB)
  55. set_tests_properties(dbWithFoo PROPERTIES FIXTURES_REQUIRED "DB;Foo")
  56. set_tests_properties(dbOnly dbWithFoo createDB setupUsers cleanupDB
  57. PROPERTIES RESOURCE_LOCK DbAccess)
  58. Key points from this example:
  59. - Two fixtures are defined: ``DB`` and ``Foo``. Tests can require a single
  60. fixture as ``fooOnly`` and ``dbOnly`` do, or they can depend on multiple
  61. fixtures like ``dbWithFoo`` does.
  62. - A ``DEPENDS`` relationship is set up to ensure ``setupUsers`` happens after
  63. ``createDB``, both of which are setup tests for the ``DB`` fixture and will
  64. therefore be executed before the ``dbOnly`` and ``dbWithFoo`` tests
  65. automatically.
  66. - No explicit ``DEPENDS`` relationships were needed to make the setup tests run
  67. before or the cleanup tests run after the regular tests.
  68. - The ``Foo`` fixture has no setup tests defined, only a single cleanup test.
  69. - ``testsDone`` is a cleanup test for both the ``DB`` and ``Foo`` fixtures.
  70. Therefore, it will only execute once regular tests for both fixtures have
  71. finished (i.e. after ``fooOnly``, ``dbOnly`` and ``dbWithFoo``). No
  72. ``DEPENDS`` relationship was specified for ``testsDone``, so it is free to
  73. run before, after or concurrently with other cleanup tests for either
  74. fixture.
  75. - The setup and cleanup tests never list the fixtures they are for in their own
  76. ``FIXTURES_REQUIRED`` property, as that would result in a dependency on
  77. themselves and be considered an error.