11

I'm not sure of how to get the nose module's __main__ handler to work. I have this at the end of my test module:

if __name__ == "__main__":
    import nose
    nose.main()

Which gives me:

----------------------------------------------------------------------
Ran 0 tests in 0.002s

OK

but it I run the same thing via the command line, it finds the tests and executes them:

MacBook-Pro:Storage_t meloam$nosetests FileManager_t.py 
............E..
======================================================================
ERROR: testStageOutMgrWrapperRealCopy (WMCore_t.Storage_t.FileManager_t.TestFileManager)
----------------------------------------------------------------------

SNIP

----------------------------------------------------------------------
Ran 15 tests in 0.082s

FAILED (errors=1)

I've been playing with passing different arguments to nose.main() but I can't find anything that works. Am I missing something really obvious?

Thanks

PerilousApricot
  • 633
  • 1
  • 7
  • 19

5 Answers5

9

For posterity's sake, this is what I use:

if __name__ == '__main__':
    import nose
    nose.run(argv=[__file__, '--with-doctest', '-vv'])

The --with-doctests will also execute your doctests in the same file.

Daniel Werner
  • 1,350
  • 16
  • 26
  • Oddly, I can't just run this from Aquamacs. Aquamacs doesn't set the __file__ variable for the temporary file into which it writes the buffer. – Charles Merriam Oct 28 '13 at 01:20
  • Giving nose the filename is smart. I was getting weird errors like it was trying to interpret the function it was in as a module name. – Brian Peterson Aug 07 '14 at 22:40
  • This seems functionally equivalent to `nose.runmodule(argv=[ 'nose', '--with-doctest', '-vv'])` – MarkHu Sep 03 '15 at 18:15
8
if __name__ == '__main__':
    import nose
    nose.run(defaultTest=__name__)
snapshoe
  • 13,454
  • 1
  • 24
  • 28
7

nose.runmodule is the way to go:

if __name__ == '__main__':
    import nose
    nose.runmodule() 
Vincent
  • 12,919
  • 1
  • 42
  • 64
1

I recommend checking 2 things:

Make sure your Source FILES follow the appropriate naming convention: (detailed in this answer).

I, for instance, had to append "_Test" to all my source files. Then, all you need is this argument (assuming your main is at the root of the tests):

nose.main(defaultTest="")

I tried with:

nose.run(defaultTest=__name__)

as a previous answer suggested, but for some reason it wasnt working for me. I had to do BOTH things to get it working!

Hope it helps.

EDIT: By the way, calling with

 nose.run() 

or

 nose.main()

made no discernible difference either.

Community
  • 1
  • 1
dgrandes
  • 1,187
  • 2
  • 14
  • 28
0

You need to use nose.core.TestProgram directly by passing it fake command line arguments. That I'm not sure though will if find your tests from the same module as you're using

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Nailor
  • 145
  • 6