首页 > 解决方案 > 为什么 nosy (nose2) 出错但 python 运行测试正常

问题描述

当我保存文件时,我试图让测试持续运行。
我试着用爱管闲事。

我可以运行我的测试python test_1.py

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

OK

但是nose2给出了

$ 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'

标签: pythonnosenose2

解决方案


我不确定,但是unittest.main()在测试内部调用会导致鼻子2出现问题吗?约定是:

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

这样另一个跑步者(例如nose2)在运行时不会调用unittest


推荐阅读