Is there a way to get python's nose module to work the same in __main__ and on the command line?

StackOverflow https://stackoverflow.com/questions/3160551

  •  01-10-2019
  •  | 
  •  

Question

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

Was it helpful?

Solution

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.

OTHER TIPS

if __name__ == '__main__':
    import nose
    nose.run(defaultTest=__name__)

nose.runmodule is the way to go:

if __name__ == '__main__':
    import nose
    nose.runmodule() 

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top