Matisla
Matisla

Reputation: 23

Split a test in different functions with pytest

I'm using pytest and have multiple tests to run to check an issue.

I would like to split all tests into different functions like this:

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

of course, I implemented issue in conftest.py

# conftest.py

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

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

but I don't know how to hook once after testing MyTestSuite and check that all tests of MyTestSuite correctly passed.

Does anyone have any ideas?

PS: this is my first post on StackOverflow.

Upvotes: 2

Views: 1682

Answers (1)

ZF007
ZF007

Reputation: 3731

Try to use the return function as most simple type of positive debug conformation as shown below.

@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

..and then where you run your tests from:

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

The result after running test1:

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

Collecting assertion errors:

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)

More assertion flavours you can find here on SO.

Upvotes: 2

Related Questions