Qing Yong
Qing Yong

Reputation: 127

How to pass by parameter for pytest

I am trying to test the methods as specified in my class. I have been doing this by global to pass into my method like,

csv.preprocess_col(df, field, remove_invalid_rows_or_raise_exception =0)

with a global df to a path.

But for now I would like to pass by parameter through functions by doing test_df(df) instead of the regular test_df() to test my functions (csv.preprocess_col) inside it but I keep getting this fixture df not found error.

Anyone knows how to solve it?

Just a curious question, for pytest is it ok to do by global to pass the parameters into the function?

Upvotes: 0

Views: 325

Answers (1)

Vincent
Vincent

Reputation: 13425

pytest supports test parametrization in several well-integrated ways:

  • pytest.fixture() allows to define parametrization at the level of fixture functions.

  • @pytest.mark.parametrize allows to define parametrization at the function or class level, provides multiple argument/fixture sets for a particular test function or class.

  • pytest_generate_tests enables implementing your own custom dynamic parametrization scheme or extensions.

A few examples here.

Upvotes: 1

Related Questions