C
C
conopus2020-07-14 21:01:07
Python
conopus, 2020-07-14 21:01:07

How to pass fixture to pytest test parameters?

The first API test checks to get a list of entities. The fixture, if successful, returns a list of entities. The second test I want to go through this list and request data for each of the entities. How to pass a list to the parameters of the second test?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Egor Ommonik, 2020-07-14
@Ommonick

It's not clear to me :)
A fixture is like prepared objects that can be used. A fixture can be either a query or an object that is returned in that query.
We abstract from fixtures. API tested? Let me rephrase your question.
The first method returns a list of entities GET /items -> response {items: [{obj},{obj},{obj}]} (so?)
And the second gives information about a specific obj by its id, for example GET /item?id=1 -> response {obj}
then we can take the items from the first one and iterate through the array, calling the second
pseudocode method for each element:

response = GET("/items")
items = response.body.items
for i, element in items {
response = GET("/item", id=element.id)
assert.equal(response.id, element[i].id) 
}

a lot depends on the implementation of the api, but most often it is like this

C
conopus, 2020-07-15
@conopus

Egor Ommonik , thank you for your reply. I agree with you and that is exactly what I ended up doing. But, this is not the answer to my question from the title.
Pytest has a very convenient fixture mechanism with their scopes and also a convenient parameterization of tests and fixtures. But, I didn’t manage to combine them: I didn’t find a way to use fixtures in the parameters. Perhaps this is an architectural limitation and cannot be bypassed. Maybe I didn't search well or there is an elegant work around. This is what I wanted to find out.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question