tests.rst 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. .. MediaGoblin Documentation
  2. Written in 2013 by MediaGoblin contributors
  3. To the extent possible under law, the author(s) have dedicated all
  4. copyright and related and neighboring rights to this software to
  5. the public domain worldwide. This software is distributed without
  6. any warranty.
  7. You should have received a copy of the CC0 Public Domain
  8. Dedication along with this software. If not, see
  9. <http://creativecommons.org/publicdomain/zero/1.0/>.
  10. ==============================
  11. Writing unit tests for plugins
  12. ==============================
  13. Here's a brief guide to writing unit tests for plugins. However, it
  14. isn't really ideal. It also hasn't been well tested... yes, there's
  15. some irony there :)
  16. Some notes: we're using py.test and webtest for unit testing stuff.
  17. Keep that in mind.
  18. My suggestion is to mime the behavior of `mediagoblin/tests/` and put
  19. that in your own plugin, like `myplugin/tests/`. Copy over
  20. `conftest.py` and `pytest.ini` to your tests directory, but possibly
  21. change the `test_app` fixture to match your own tests' config needs.
  22. For example::
  23. import pkg_resources
  24. # [...]
  25. @pytest.fixture()
  26. def test_app(request):
  27. return get_app(
  28. request,
  29. mgoblin_config=pkg_resources.resource_filename(
  30. 'myplugin.tests', 'myplugin_mediagoblin.ini'))
  31. In any test module in your tests directory you can then do::
  32. def test_somethingorother(test_app):
  33. # real code goes here
  34. pass
  35. And you'll get a mediagoblin application wrapped in webtest passed in
  36. to your environment.
  37. If your plugin needs to define multiple configuration setups, you can
  38. actually set up multiple fixtures very easily for this. You can just
  39. set up multiple fixtures with different names that point to different
  40. configs and pass them in as that named argument.
  41. To run the tests, from mediagoblin's directory (make sure that your
  42. plugin has been added to your mediagoblin checkout's virtualenv!) do::
  43. ./runtests.sh /path/to/myplugin/tests/
  44. replacing `/path/to/myplugin/` with the actual path to your plugin.
  45. NOTE: again, the above is untested, but it should probably work. If
  46. you run into trouble, `contact us
  47. <http://mediagoblin.org/pages/join.html>`_, preferably on IRC!