Hide
Meet people who work on similar things as you – get help if you need it Join Siafoo Now or Learn More

Add a Command Line Option to Nosetests to Run 'setup-app' in Paste Atom Feed 0

In Brief A simple nosetests plugin. Adds a command-line option to runs Paste's 'setup-app' function before tests are run. Also usable as a template for building other plugins.... more
# 's
 1# Written by David Isaacson
2# Copyright 2008 Siafoo.net
3# BSD License
4
5import os
6from nose.plugins import Plugin
7
8class MyAppPlugin(Plugin):
9 name = 'my_app'
10 enabled = False
11
12 def add_options(self, parser, env=os.environ):
13 '''Add command-line options for plugin'''
14 env_opt = 'NOSE_PASTE_SETUP_FIRST'
15
16 parser.add_option('--paste-setup-first',
17 action='store_true',
18 default=env.get('env_opt'),
19 dest='paste_setup',
20 help='Run setup-app in Paste before running tests. [%s]' % env_opt)
21
22 def configure(self, options, conf):
23 """Configure the plugin"""
24 Plugin.configure(self, options, conf)
25 if options.paste_setup:
26 self.enabled = True
27
28 def begin(self):
29 '''Called before any tests are collected or run. Resets database.'''
30 from paste.script.appinstall import SetupCommand
31
32 # Select the .ini file to run setup-app on
33 test_file = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'test.ini')
34 SetupCommand('setup-app').run([test_file])

A simple nosetests plugin. Adds a command-line option to runs Paste's 'setup-app' function before tests are run. Also usable as a template for building other plugins.

If --paste-setup-first is given, nosetests runs the 'begin' function, which calls 'setup-app' on the file test_file. On Siafoo, this drops all tables from the test database and recreates the database. We use this mode for automated tests.

To install the plugin, add:

[nose.plugins]
my_app = path.to.plugin:MyPlugin

To your entry_points in your setup.py. And run setup.py of course.

For a full tutorial on adding command-line options to nosetests, check out the article: Add Command-Line Options to Nosetests