使用 pytest 拆分不同函数的测试

Split a test in different functions with pytest

我正在使用 pytest 并进行了多项测试以 运行 检查问题。

我想将所有测试分成不同的功能,如下所示:

# test_myTestSuite.py

@pytest.mark.issue(123)
class MyTestSuite():

    def test_part_1():
        result = do_something()
        assert result == True

    def test_part_2():
        result = do_an_other_something()
        assert result == 'ok'

当然,我在conftest.py

中实现了issue
# conftest.py

def pytest_addoption(parser):
    group = parser.getgroup('Issues')

    group.addoption('--issue', action='store',
                    dest='issue', default=0,
                    help='')

但我不知道如何在测试 MyTestSuite 后挂钩一次并检查 MyTestSuite 的所有测试是否正确通过。

有没有人有什么想法?

PS:这是我在 Whosebug 上的第一个 post。

尝试使用 return 函数作为最简单的正调试构造类型,如下所示。

@pytest.mark.issue(123)
class MyTestSuite():

    def test_part_1():
        result = do_something()
        assert result == True
        return 'tp1', True

    def test_part_2():
        result = do_an_other_something()
        assert result == 'ok'
        return 'tp2', True

..然后你 运行 你的测试来自:

x = MyTestSuite().test_part_1()
if x[1] == True:
    print 'Test %s completed correctly' % x[0] 

结果 运行ning test1:

  1. Test tp1 completed correctly, or...
  2. AssertionError.

正在收集断言错误:

collected_errors = []

def test_part_1():
    testname = 'tp1'
    try:
        result = do_something()
        assert result == True
        return testname, True
    except Exception as error:
        info = (testname, error)
        collected_errors.append(info)

您可以在 here 上找到更多断言风格。