FIXTURES_REQUIRED.rst 4.8 KB

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