2

I'm trying to get tests running continually when I save a file.
I'm try to use nosy.

I can run my tests with python test_1.py

$ python test_1.py
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

but nose2 gives

$ nose2

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK
E
======================================================================
ERROR: test_1 (nose2.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: test_1
Traceback (most recent call last):
  File "/home/durrantm/.local/lib/python3.7/site-packages/nose2/plugins/loader/discovery.py", line 201, in _find_tests_in_file
    module = util.module_from_name(module_name)
  File "/home/durrantm/.local/lib/python3.7/site-packages/nose2/util.py", line 77, in module_from_name
    __import__(name)
  File "/home/durrantm/Dropbox/90_2019/work/code/pair_and_mob/python/for_nose_2/test_1.py", line 10, in <module>
    unittest.main()
  File "/usr/lib/python3.7/unittest/main.py", line 101, in __init__
    self.runTests()
  File "/usr/lib/python3.7/unittest/main.py", line 273, in runTests
    sys.exit(not self.result.wasSuccessful())
SystemExit: False


----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

$ cat test_1.py 
import unittest
from mycode import *
class MyFirstTests(unittest.TestCase):
  def test_hello(self):
    self.assertEqual(hello_world(), 'hello world')
  def test_goodbye(self):
    self.assertEqual(goodbye_world(), 'goodbye world')
unittest.main()

$ cat mycode.py
def hello_world():
  return 'hello world'
def goodbye_world():
  return 'goodbye world'
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497

1 Answers1

1

I'm not certain, but is calling unittest.main() inside the test causing problems for nose2? The convention is:

if __name__ == "__main__":
  unittest.main()

so that another runner (e.g. nose2) doesn't invoke unittest when it runs.

Peter Dowdy
  • 439
  • 2
  • 16
  • This solved my issue. I had `unittest.main()` in the global scope of my test file. After removing it, nose2 worked fine. – Bunny Nov 26 '22 at 08:29