[英]pytest: run test from code, not from command line
Is it possible to run tests from code using pytest? 是否可以使用pytest从代码运行测试? I did find pytest.main
, but it's just a command line interface available from code. 我确实找到了pytest.main
,但它只是代码中提供的命令行界面。 I would like to pass a test class / function from the code. 我想从代码中传递一个测试类/函数。
In unittest it's possible this way: 在unittest中,这可能是这样的:
from unittest import TestLoader, TestCase, TestResult
class TestMy(TestCase):
def test_silly(self):
assert False
runner = TestLoader()
test_suite = runner.loadTestsFromTestCase(TestMy)
test_result = TestResult()
test_suite.run(test_result)
print(test_result)
Yes it's possible, that way for instance: 是的,有可能,例如:
from pytest import main
class TestMy:
def test_silly(self):
assert False
main(['{}::{}'.format(__file__, TestMy.__name__)])
You can pass any argument to main
as if called from command line. 您可以将任何参数传递给main
就像从命令行调用一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.