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