I am new to end-to-end testing. I decided to use pytest and fixtures, but I have a problem in giving parameters to my fixture method. I have a method called authorize, where I push an api key and a password and it returns base64 authorization, which I use in my get method. Also, if the api key and the pass are not given in the authorize method, it prints a message that asks the user to input them manually. The problem is that when I try to push the api key and the pass directly to authorization like this -> authorization("MyApiKey","Test123"), the program gives an error
Fixture "authorization" called directly. Fixtures are not meant to be called directly, but are created automatically when test functions request them as parameters.
When I do the following my_auth = authorization, "MyAPIKey", "Test123" an error does not occur, but unfortunately it doesn't take these parameters and wants me to enter new data manually. How can I solve this problem?
@pytest.fixture(scope="class")
def authorization(API_KEY="", password=""):
if API_KEY and password:
user_data = authorize(API_KEY, password)
else:
user_data = authorize()
return user_data
class Test_create_fixture():
@pytest.mark.usefixtures(name=['authorization'])
def test_get_empl(self, authorization):
my_auth = authorization, "MyAPIKey", "Test123"
response = get('https://w3schools.com', my_auth)
assert response.status_code == 200
As the error states, the fixtures are created automatically, so you can't and shouldn't directly pass arguments to them. Note that fixtures can be chained, i.e. the arguments to one fixtures may be other fixtures.
In order to pass the arguments you must read it from other source, here are few options:
Using my_auth = authorization, "MyAPIKey", "Test123"
works because it's actually harcoding the parameters to the methods arguments