12

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?

Matt W
  • 6,078
  • 3
  • 32
  • 40

2 Answers2

18

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!

dbn
  • 13,144
  • 3
  • 60
  • 86
  • 1
    I believe this is the more correct answer. The point about Nose being sneaky with the first argument is a very important point. – munk Feb 27 '13 at 20:54
  • 6
    nose is not sneaky :) The point is that `argv` list always starts with the name of the executable. Thus, when you say `argv = ['--with-xunit']`, you set the name of the executable to `--with-xunit`, and pass no arguments. To fix this, use `argv = ['fake', '--with-xunit']` – Tim Sep 27 '13 at 08:48
  • I suppose this was my implicit point, thanks for making it explicit. The first "argument" is used to make suer that help is printed correctly, etc. – dbn Sep 27 '13 at 19:59
8

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.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
mouad
  • 67,571
  • 18
  • 114
  • 106
  • @Matt: Are you sure ? because i'm using the same snippet of code in one my project, and i just changed now to generate an `xunit` report and it was generated successfully under `nosetests.xml` !? maybe i'm missing something ... – mouad Aug 15 '11 at 22:02
  • Yeah, I tried this first and a report was not generated. Tried with command line and I got one. Sorry :-/ – Matt W Aug 15 '11 at 22:12
  • I use `nose.core.run()` to run nose programmatically. Not sure what the difference is to `main()`. – Santa Aug 15 '11 at 22:24
  • @Santa: you can read it in the docstring of the two function here what it say: `The arguments to run() are the same as to main(): ... With the exception that the ``exit`` argument in run() is always set to False` and in main it's set by default to True which mean that nose will exit after running tests and printing report. – mouad Aug 16 '11 at 11:31
  • 2
    I think I know why this didn't work. Nose does not use the first argument to argv to configure itself. This may have also caused problems with parallel execution? – dbn Dec 15 '12 at 02:48
  • 1
    argv = ['fake_executable_name', '--with-xunit'] will fix the problem – Tim Sep 27 '13 at 08:52
  • First element of `sys.argv` is the scripts filename, so try to use `argv = [sys.argv[0], '--with-xunit']`. – Wolfgang Ulmer Aug 04 '14 at 07:28
  • Or even better: `sys.argv.extend(['--with-xunit']); nose.main(argv=sys.argv)`. – Wolfgang Ulmer Aug 04 '14 at 07:30
  • This answer should be modified to include and empty first arg. It is not correct. – Clintm Apr 02 '15 at 20:45