Commit | Line | Data |
---|---|---|
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 | ============================== | |
15 | Writing unit tests for plugins | |
16 | ============================== | |
17 | ||
18 | Here's a brief guide to writing unit tests for plugins. However, it | |
19 | isn't really ideal. It also hasn't been well tested... yes, there's | |
20 | some irony there :) | |
21 | ||
9650aa39 | 22 | Some notes: we're using py.test and WebTest for unit testing stuff. |
25aad338 CAW |
23 | Keep that in mind. |
24 | ||
25 | My suggestion is to mime the behavior of `mediagoblin/tests/` and put | |
26 | that in your own plugin, like `myplugin/tests/`. Copy over | |
27 | `conftest.py` and `pytest.ini` to your tests directory, but possibly | |
28 | change the `test_app` fixture to match your own tests' config needs. | |
29 | For 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 | ||
41 | In 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 | 47 | And you'll get a MediaGoblin application wrapped in WebTest passed in |
25aad338 CAW |
48 | to your environment. |
49 | ||
50 | If your plugin needs to define multiple configuration setups, you can | |
51 | actually set up multiple fixtures very easily for this. You can just | |
52 | set up multiple fixtures with different names that point to different | |
53 | configs and pass them in as that named argument. | |
54 | ||
9650aa39 BS |
55 | To run the tests, from MediaGoblin's directory (make sure that your |
56 | plugin has been added to your MediaGoblin checkout's virtualenv!) do:: | |
25aad338 CAW |
57 | |
58 | ./runtests.sh /path/to/myplugin/tests/ | |
59 | ||
60 | replacing `/path/to/myplugin/` with the actual path to your plugin. | |
61 | ||
62 | NOTE: again, the above is untested, but it should probably work. If | |
63 | you run into trouble, `contact us | |
64 | <http://mediagoblin.org/pages/join.html>`_, preferably on IRC! |