Passing options to nose in a Python test script

2019-02-06 13:00发布

Rather than running my nose tests from the command line, I'm using a test runner that sets up a few things for all the tests, including a connection to a local test instance of MongoDB. The documentation for nose only seems to indicate how to pass options through the command line or a configuration file located in your home directory. Is there a way to pass options, such as --with-xunit when using a script to run your tests?

标签: python nose
2条回答
Deceive 欺骗
2楼-- · 2019-02-06 13:19

Like this:

import nose

argv = ['fake', '--with-xunit']
nose.main(argv=argv)

The "fake" argument must be added to stand in for the executable name, as described in dbw's answer.

查看更多
何必那么认真
3楼-- · 2019-02-06 13:23

Nose does something sneaky with the first argument, so it is not parsed. My nose wrapper does something like this:

import nose
import sys

argv = sys.argv[:]
argv.insert(1, "--with-xunit")
nose.main(argv=argv)

As a bonus, this allows the clients of your program to use Nose arguments to control its behavior!

查看更多
登录 后发表回答