Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytest setup and teardown functions - same as self written functions?

Tags:

python

pytest

In the following example from pytest documentation:

enter image description here

The function setup_function is supposed to setup some data for some other function, say test_data. So If i write the function test_data i will have to invoke setup_function like this:

def test_data():
    setup_function(....)
    <Test logic here>
    teardown_function(....)

So the only difference is name convention ?

I don't understand how exactly is helps me creating setup data. I could have written the same code like this:

def test_data():
    my_own_setup_function(....)
    <Test logic here>
    my_own_teardown_function(....)

Since there is no way to tell pytest to automatically link a setup function to the test function it creates the setup data for - the parameter function of the function setup_function doesn't really help me if i don't need a function pointer.... so why bother creating name conventions for no reason?

From what i understand, the setup function parameter function only helps me if i need to use a function pointer - something i rarely need.

like image 943
Caffeine Avatar asked Dec 10 '22 05:12

Caffeine


2 Answers

If you want to setup specifics for a test or tests you can use a "normal" pytext fixture.

import pytest

@pytest.fixture
def setup_and_teardown_for_stuff():
    print("\nsetting up")
    yield
    print("\ntearing down")

def test_stuff(setup_and_teardown_for_stuff):
    assert 1 == 2

The thing to remember is that everyting before the yield is run before the test and everything after the yield is run after the test.

tests/unit/test_test.py::test_stuff 
setting up
FAILED
tearing down
like image 139
Leon Avatar answered Dec 12 '22 19:12

Leon


Answer

It appears that your question boils down to: What is the purpose/benefit of the setup_function and teardown_function as described in the pytest documentation?

The benefit of using these functions is that you do not have to invoke them; both setup_function and teardown_function will automatically run before and after (respectively) each test.

To your point about having to pass a function pointer, this is not required in pytest>=3.0. From the documentation:

As of pytest-3.0, the function parameter is optional.

So you don't need to pass a function pointer to the setup_function and teardown_function functions; you can simply add them to a test file as described in the example below and they will be executed.


Example

For example, if you have a test_setup_teardown.py file that looks like:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-


def setup_function():
    print('setting up')


def test_1():
    print('test 1')
    assert 1 == 2


def teardown_function():
    print('tearing down')

and you run that file using pytest (something like pytest test_setup_teardown.py), pytest will output:

---- Captured stdout setup ----
setting up
---- Captured stdout call ----
test 1
---- Captured stdout teardown ----
tearing down

In other words, pytest automatically calls the setup_function, then runs the test (which fails), and then runs the teardown_function. The benefit of these functions is being able to specify what happens before and after all tests are run.

like image 35
Floyd Avatar answered Dec 12 '22 19:12

Floyd