PageRenderTime 56ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/tests.rst

https://gitlab.com/jslee1/kitsune
ReStructuredText | 257 lines | 174 code | 83 blank | 0 comment | 0 complexity | ab4711416218ff3f2bdffb15997eb524 MD5 | raw file
  1. .. _tests-chapter:
  2. =================
  3. All about testing
  4. =================
  5. Kitsune has a fairly comprehensive Python test suite. Changes should
  6. not break tests---only change a test if there is a good reason to
  7. change the expected behavior---and new code should come with tests.
  8. Running the Test Suite
  9. ======================
  10. If you followed the steps in :ref:`the installation docs
  11. <hacking-howto-chapter>`, then you should be all set setup-wise.
  12. To run the tests, you need to do::
  13. ./manage.py test
  14. That doesn't provide the most sensible defaults for running the
  15. tests. Here is a good command to alias to something short::
  16. ./manage.py test -s --noinput --logging-clear-handlers
  17. The ``-s`` flag is important if you want to be able to drop into PDB from
  18. within tests.
  19. Some other helpful flags are:
  20. ``-x``:
  21. Fast fail. Exit immediately on failure. No need to run the whole test suite
  22. if you already know something is broken.
  23. ``--pdb``:
  24. Drop into PDB on an uncaught exception. (These show up as ``E`` or errors in
  25. the test results, not ``F`` or failures.)
  26. ``--pdb-fail``:
  27. Drop into PDB on a test failure. This usually drops you right at the
  28. assertion.
  29. ``--no-skip``:
  30. All SkipTests show up as errors. This is handy when things shouldn't be
  31. skipping silently with reckless abandon.
  32. Running a Subset of Tests
  33. -------------------------
  34. You can run part of the test suite by specifying the apps you want to run,
  35. like::
  36. ./manage.py test kitsune/wiki kitsune/search kitsune/kbforums
  37. You can also specify modules::
  38. ./manage.py test kitsune.wiki.tests.test_views
  39. You can specify specific tests::
  40. ./manage.py test kitsune.wiki.tests.test_views:VersionGroupTests.test_version_groups
  41. See the output of ``./manage.py test --help`` for more arguments.
  42. Running tests without collecting static files
  43. ---------------------------------------------
  44. By default the test runner will run ``collectstatic`` to ensure that all the required assets have
  45. been collected to the static folder. If you do not want this default behavior you can run::
  46. REUSE_STATIC=1 ./manage.py test
  47. The Test Database
  48. -----------------
  49. The test suite will create a new database named ``test_%s`` where
  50. ``%s`` is whatever value you have for
  51. ``settings.DATABASES['default']['NAME']``.
  52. Make sure the user has ``ALL`` on the test database as well. This is
  53. covered in the installation chapter.
  54. When the schema changes, you may need to drop the test database. You
  55. can also run the test suite with ``FORCE_DB`` once to cause Django to
  56. drop and recreate it::
  57. FORCE_DB=1 ./manage.py test -s --noinput --logging-clear-handlers
  58. Writing New Tests
  59. =================
  60. Code should be written so it can be tested, and then there should be
  61. tests for it.
  62. When adding code to an app, tests should be added in that app that
  63. cover the new functionality. All apps have a ``tests`` module where
  64. tests should go. They will be discovered automatically by the test
  65. runner as long as the look like a test.
  66. * If you're expecting ``reverse`` to return locales in the URL, use
  67. ``LocalizingClient`` instead of the default client for the
  68. ``TestCase`` class.
  69. * We use "modelmakers" instead of fixtures. Models should have
  70. modelmakers defined in the tests module of the Django app. For
  71. example, ``forums.tests.document`` is the modelmaker for
  72. ``forums.Models.Document`` class.
  73. Changing Tests
  74. ==============
  75. Unless the current behavior, and thus the test that verifies that
  76. behavior is correct, is demonstrably wrong, don't change tests. Tests
  77. may be refactored as long as its clear that the result is the same.
  78. Removing Tests
  79. ==============
  80. On those rare, wonderful occasions when we get to remove code, we
  81. should remove the tests for it, as well.
  82. If we liberate some functionality into a new package, the tests for
  83. that functionality should move to that package, too.
  84. JavaScript Tests
  85. ================
  86. Frontend JavaScript is currently tested with Mocha_.
  87. Running JavaScript Tests
  88. ------------------------
  89. To run tests, make sure you have have the NPM dependencies installed, and
  90. then run::
  91. $ scripts/mocha.sh
  92. Writing JavaScript Tests
  93. ------------------------
  94. Mocha tests are discovered using the pattern
  95. ``kitsune/*/static/*/js/tests/**/*.js``. That means that any app can
  96. have a `tests` directory in its JavaScript directory, and the files in
  97. there will all be considered test files. Files that don't define tests
  98. won't cause issues, so it is safe to put testing utilities in these
  99. directories as well.
  100. Here are a few tips for writing tests:
  101. * Any HTML required for your test should be added by the tests or a
  102. ``beforeEach`` function in that test suite. React is useful for this.
  103. * You can use `sinon` to mock out parts of libraries or functions under
  104. test. This is useful for testing AJAX.
  105. * The tests run in a Node.js environment. A browser environment can be
  106. simulated using ``jsdom``. Specifically, ``mocha-jsdom`` is useful to
  107. set up and tear down the simulated environment.
  108. .. _Mocha: https://mochajs.org/
  109. Functional UI Tests
  110. ===================
  111. We can do more comprehensive front-end testing with the functional UI tests.
  112. They're located in the ``tests/functional`` directory.
  113. Installing dependencies
  114. -----------------------
  115. Follow the steps in :ref:`the installation docs <hacking-howto-chapter>`,
  116. including the test dependencies to make sure you have everything you need to
  117. run the tests. If you're running the tests against a deployed environment then
  118. there's no need to install anything other than the test dependencies.
  119. Create test users
  120. -----------------
  121. Some of the tests require logging in as a administrator, and others require
  122. logging in as a user. To run these tests you will need to create accounts in
  123. the target environment. If you're running against a local instance of the
  124. application you can create these users by running the following script::
  125. $ ./manage.py shell < ./scripts/create_user_and_superuser.py
  126. If you want to run the tests that require administrator access against a
  127. deployed instance, then you will need to ask someone on IRC to upgrade one of
  128. your test accounts.
  129. The credentials associated with the test users are stored in a JSON file, which
  130. we then pass to the tests via the command line. If you used the above mentioned
  131. script, then these users are stored in ``/scripts/travis/variables.json``. The
  132. variable file needs to be referenced on the command line when running the
  133. tests.
  134. The following is an example JSON file with the values missing. You can use this
  135. as a template:
  136. .. code:: json
  137. {
  138. "users": {
  139. "default": {
  140. "username": "",
  141. "password": "",
  142. "email": ""},
  143. "admin": {
  144. "username": "",
  145. "password": "",
  146. "email": ""}
  147. }
  148. }
  149. For the purposes of the examples below, assume you named your copy of the file
  150. ``my_variables.json``.
  151. Running the tests
  152. -----------------
  153. Tests are run using the command line. Below are a couple of examples of running
  154. the tests:
  155. To run all of the desktop tests against the default environment::
  156. $ py.test --driver Firefox --variables my_variables.json tests/functional/desktop
  157. To run against a different environment, pass in a value for ``--base-url``,
  158. like so::
  159. $ py.test --base-url https://support.allizom.org --driver Firefox --variables my_variables.json tests/functional/desktop
  160. To run the mobile tests you will need to target a mobile device or emulator
  161. using a tool like `Appium <http://appium.io/>`_::
  162. $ py.test --driver Remote --port 4723 \
  163. --capability platformName iOS \
  164. --capability platformVersion 9.2 \
  165. --capability deviceName "iPhone 6" \
  166. --capability browserName Safari \
  167. --variables my_variables.json \
  168. tests/functional/mobile
  169. Alternatively, if you run the mobile tests in Firefox the user agent will be
  170. changed to masquerade as a mobile browser.
  171. The pytest plugin that we use for running tests has a number of advanced
  172. command line options available. To see the options available, run
  173. ``py.test --help``. The full documentation for the plugin can be found
  174. `here <https://pytest-selenium.readthedocs.io/>`_.