sách gpt4 ai đã đi

Python 单元测试 : In Nose is there a way to skip a test case from nose. run()?

In lại 作者:太空宇宙 更新时间:2023-11-04 03:35:06 29 4
mua khóa gpt4 Nike

我正在编写一组测试用例,比如测试模块中的 Test1、Test2。

有没有办法使用命令 nose.main() 在该模块中跳过 Test1 或选择性地仅执行 Test2?

我的模块包含,

测试模块.py,

class Test1:
setUp(self):
print('setup')
tearDown(self):
print('teardown')
test(self):
print('test1')

class Test2:
setUp(self):
print('setup')
tearDown(self):
print('teardown')
test(self):
print('test2')

我使用不同的 python 文件运行它,

nếu __name__ == '__main__':
nose.main('test_module')

1 Câu trả lời

跳过测试和不运行测试的概念在 nose 上下文中是不同的:跳过的测试将在测试结果结束时报告为跳过。如果你想跳过测试,你将不得不用装饰器猴子修补你的测试模块或做一些其他的黑魔法。

但是如果你不想运行测试,你可以像在命令行中那样做:使用--exclude 选项。它采用您不想运行的测试的正则表达式。像这样:

nhập khẩu hệ thống
import nose

def test_number_one():
vượt qua

def test_number_two():
vượt qua

nếu __name__ == '__main__':
module_name = sys.modules[__name__].__file__

nose.main(argv=[sys.argv[0],
module_name,
'--exclude=two',
'-v'
])

运行测试会给你:

$ python stackoverflow.py
stackoverflow.test_number_one ... ok

-----------------------------------------------------------------------------------
Ran 1 test in 0.002s

ĐƯỢC RỒI

关于Python 单元测试 : In Nose is there a way to skip a test case from nose. run()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29397973/

29 4 0
太空宇宙
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com