Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytest - helper function or fixture, parametrization

pytest setup is mostly based on fixtures and I like this approach because it's more atomic, explicit and less limiting that class inheritance in xUnit. Is there any convention or guideline to make fixtures instead helper functions?1 Consider the code:

def create_flavored_cake(flavor):
    return Cake(flavor)

def test_chocolate():
    cake = create_flavored_cake('chocolate')
    assert cake.is_yummy()

def test_broccoli():
    cake = create_flavored_cake('broccoli')
    assert cake.is_yummy()

def test_broccoli_with_risins():
    cake = create_flavored_cake('broccoli')
    cake.add('risins')
    assert cake.is_yummy()

Should create_flavored_cake be transformed to a fixture?2 Does such scenario, where tests have identical statements but different parameters, should trigger me to transform helper function to fixture to allow easier parametrization and reduce duplication?3 Should I use fixtures only for setup and teardown?4 Or it doesn't matter and by convention, if any, it should use fixture from the start?5

I don't really like parametrizing fixtures by indirect, but it seems there is not better way. Is that because it's a bad practice?6 I'm aware that I can also use pytest.mark.parametrize which will look better IMO. So where I should draw the lines between using fixture, helper function and some other approach (if there is a better solution) also taking into consideration parametrizing them?7

I've added superscripts for question reference. You don't have to give anwsers to all of them.

like image 333
WloHu Avatar asked Aug 24 '18 10:08

WloHu


People also ask

Can you parameterize a pytest fixture?

pytest enables test parametrization at several levels: pytest. fixture() allows one to parametrize fixture functions.

What is pytest fixture ()?

pytest fixtures are functions attached to the tests which run before the test function is executed. Fixtures are a set of resources that have to be set up before and cleaned up once the Selenium test automation execution is completed.

Why fixture is used in pytest?

Fixtures are used to feed some data to the tests such as database connections, URLs to test and some sort of input data. Therefore, instead of running the same code for every test, we can attach fixture function to the tests and it will run and return the data to the test before executing each test.


1 Answers

In the case that I have to do this, I just create a fixture that returns my function. I agree that the indirect method just seems to create ugly code.

@pytest.fixture
def generate_data():
    def generate_data_function(some_parameters):
        # Code goes here
    return generate_data_function

def test_something(generate_data):
    my_test_parameters = # something
    generate_data(my_test_parameters)

    # Do test...

As far as good practice goes, at least this way I can quickly see if a test is using a helper function, rather than importing it from a module for the whole file and only using it in some tests.

like image 169
mwaddoups Avatar answered Oct 19 '22 19:10

mwaddoups