python - 为什么 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'
解决方案
我不确定,但是unittest.main()
在测试内部调用会导致鼻子2出现问题吗?约定是:
if __name__ == "__main__":
unittest.main()
这样另一个跑步者(例如nose2)在运行时不会调用unittest
。
推荐阅读
- javascript - mongoose - 根据鉴别器将“父”类型转换为“子”类型
- entity-framework - EF Fluent API - 复杂字段的 HasIndex
- java - 如何在 RecyclerView Scroll 上下载更多 Firebase 对象?
- php - 给定的对象管理器不由包装器管理
- laravel - Laravel 5.5 Api 注册问题
- xml - 对类别不在参考列表中的元素进行排序
- node.js - 当我运行“npm run ng new app”时,谁能解释这个错误?
- r - R中带有双条件的多条件
- c++11 - Boost 套接字 send_to() 问题
- go - 如何在 lambda 函数中引用其他文件?