Writing unit tests for plugins

Here’s a brief guide to writing unit tests for plugins. However, it isn’t really ideal. It also hasn’t been well tested… yes, there’s some irony there :)

Some notes: we’re using py.test and webtest for unit testing stuff. Keep that in mind.

My suggestion is to mime the behavior of mediagoblin/tests/ and put that in your own plugin, like myplugin/tests/. Copy over conftest.py and pytest.ini to your tests directory, but possibly change the test_app fixture to match your own tests’ config needs. For example:

import pkg_resources
# [...]

@pytest.fixture()
def test_app(request):
    return get_app(
        request,
        mgoblin_config=pkg_resources.resource_filename(
            'myplugin.tests', 'myplugin_mediagoblin.ini'))

In any test module in your tests directory you can then do:

def test_somethingorother(test_app):
    # real code goes here
    pass

And you’ll get a mediagoblin application wrapped in webtest passed in to your environment.

If your plugin needs to define multiple configuration setups, you can actually set up multiple fixtures very easily for this. You can just set up multiple fixtures with different names that point to different configs and pass them in as that named argument.

To run the tests, from mediagoblin’s directory (make sure that your plugin has been added to your mediagoblin checkout’s virtualenv!) do:

./runtests.sh /path/to/myplugin/tests/

replacing /path/to/myplugin/ with the actual path to your plugin.

NOTE: again, the above is untested, but it should probably work. If you run into trouble, contact us, preferably on IRC!
{"cc_re__cc_re__cc_publicdomain_zero_1_0": ["   >modules</a> |</li>\n        <li class=\"right\" >\n          <a href=\"hooks.html\" title=\"Documentation on Built-in Hooks\"\n             >next</a> |</li>\n        <li class=\"right\" >\n          <a href=\"api.html\" title=\"Plugin API\"\n             >previous</a> |</li>\n        <li class=\"nav-item nav-item-0\"><a href=\"../index.html\">GNU MediaGoblin 0.9.0 documentation</a> &#187;</li> \n      </ul>\n    </div>\n    <div class=\"footer\">\n    <div>\n\nMediaGoblin documentation released into the public domain via <a href=\"http://creativecommons.org/publicdomain/zero/1.0/\">CC0</a>.\n\n    Created using <a href=\"http://sphinx.pocoo.org/\">Sphinx</a> 1.6.5.\n    </div>\n    </div>\n  </body>\n</html>"]}