qubes-tests.rst 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. :py:mod:`qubes.tests` -- Writing tests for qubes
  2. ================================================
  3. Writing tests is very important for ensuring quality of code that is delivered.
  4. Given test case may check for variety of conditions, but they generally fall
  5. inside those two categories of conformance tests:
  6. * Unit tests: these test smallest units of code, probably methods of functions,
  7. or even combination of arguments for one specific method.
  8. * Integration tests: these test interworking of units.
  9. We are interested in both categories.
  10. There is also distinguished category of regression tests (both unit- and
  11. integration-level), which are included because they check for specific bugs that
  12. were fixed in the past and should not happen in the future. Those should be
  13. accompanied with reference to closed ticked that describes the bug.
  14. Qubes' tests are written using :py:mod:`unittest` module from Python Standard
  15. Library for both unit test and integration tests.
  16. Test case organisation
  17. ----------------------
  18. Every module (like :py:mod:`qubes.vm.qubesvm`) should have its companion (like
  19. ``qubes.tests.vm.qubesvm``). Packages ``__init__.py`` files should be
  20. accompanied by ``init.py`` inside respective directory under :file:`tests/`.
  21. Inside tests module there should be one :py:class:`qubes.tests.QubesTestCase`
  22. class for each class in main module plus one class for functions and global
  23. variables. :py:class:`qubes.tests.QubesTestCase` classes should be named
  24. ``TC_xx_ClassName``, where ``xx`` is two-digit number. Test functions should be
  25. named ``test_xxx_test_name``, where ``xxx`` is three-digit number. You may
  26. introduce some structure of your choice in this number.
  27. FIXME: where are placed integration tests?
  28. Writing tests
  29. -------------
  30. First of all, testing is art, not science. Testing is not panaceum and won't
  31. solve all of your problems. Rules given in this guide and elsewhere should be
  32. followed, but shouldn't be worshipped.
  33. When writing test, you should think about order of execution. Tests should be
  34. written bottom-to-top, that is, tests that are ran later may depend on features
  35. that are tested after but not the other way around. This is important, because
  36. when encountering failure we expect the reason happen *before*, and not after
  37. failure occured. Therefore, when encountering multiple errors, we may instantly
  38. focus on fixing the first one and not wondering if any later problems may be
  39. relevant or not. This is the reason of numbers in names of the classes and test
  40. methods.
  41. You may, when it makes sense, manipulate private members of classes under tests.
  42. This violates one of the founding principles of object-oriented programming, but
  43. may be required to write tests in correct order if your class provides public
  44. methods with circular dependencies. For example containers may check if added
  45. item is already in container, but you can't test ``__contains__`` method without
  46. something already inside. Don't forget to test the other method later.
  47. Special Qubes-specific considerations
  48. -------------------------------------
  49. Events
  50. ^^^^^^
  51. :py:class:`qubes.tests.QubesTestCase` provides convenient methods for checking
  52. if event fired or not: :py:meth:`qubes.tests.QubesTestCase.assertEventFired` and
  53. :py:meth:`qubes.tests.QubesTestCase.assertEventNotFired`. These require that
  54. emitter is subclass of :py:class:`qubes.tests.TestEmitter`. You may instantiate
  55. it directly::
  56. import qubes.tests
  57. class TC_10_SomeClass(qubes.tests.QubesTestCase):
  58. def test_000_event(self):
  59. emitter = qubes.tests.TestEmitter()
  60. emitter.fire_event('did-fire')
  61. self.assertEventFired(emitter, 'did-fire')
  62. If you need to snoop specific class (which already is a child of
  63. :py:class:`qubes.events.Emitter`, possibly indirect), you can define derivative
  64. class which uses :py:class:`qubes.tests.TestEmitter` as mix-in::
  65. import qubes
  66. import qubes.tests
  67. class TestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  68. pass
  69. class TC_20_PropertyHolder(qubes.tests.QubesTestCase):
  70. def test_000_event(self):
  71. emitter = TestHolder()
  72. self.assertEventNotFired(emitter, 'did-not-fire')
  73. Dom0
  74. ^^^^
  75. Qubes is a complex piece of software and depends on number other complex pieces,
  76. notably VM hypervisor or some other isolation provider. Not everything may be
  77. testable under all conditions. Some tests (mainly unit tests) are expected to
  78. run during compilation, but many tests (probably all of the integration tests
  79. and more) can run only inside already deployed Qubes installation. There is
  80. special decorator, :py:func:`qubes.tests.skipUnlessDom0` which causes test (or
  81. even entire class) to be skipped outside dom0. Use it freely::
  82. import qubes.tests
  83. class TC_30_SomeClass(qubes.tests.QubesTestCase):
  84. @qubes.tests.skipUnlessDom0
  85. def test_000_inside_dom0(self):
  86. # this is skipped outside dom0
  87. pass
  88. @qubes.tests.skipUnlessDom0
  89. class TC_31_SomeOtherClass(qubes.tests.QubesTestCase):
  90. # all tests in this class are skipped
  91. pass
  92. Module contents
  93. ---------------
  94. .. automodule:: qubes.tests
  95. :members:
  96. :show-inheritance:
  97. .. vim: ts=3 sw=3 et tw=80