❔ Writting tests for api calls
i am writing a test for api calls on an external web api. If i have one post test that creates something, is it bad practice to base my get test on the data created from the previous post test?
11 Replies
should i run the create as well in the get test instead?
u should not make tests dependent on each other.
usually u would have a test database filled with test data, so u can purely test each unit by itself.
often such data bases are non persistent docker images, so to to start the test, u start the test db container, run the tests and then shutdown the container again.
i think i explained it wrong. To be more specific. I use the azure dev ops api provided by microsoft.
My code creates (POST) a personal access token and can also GET them for example.
Now i want to write an integration? test for it by using the real api calls.
But before i can GET a token i would have to make sure there is one first (POST).
My question is, if i have a test that does the POST. Can't i use the response from that one for the GET test?
or do i have a fundamental misunderstanding of how you do tests like this
with response i mean, the post call gives me an ID which i can use for the GET
hm, to be honest, on that i dont have any experience 😒
ah ok, np. having a hard time wrapping my head around this
i would guess its okay to reuse that access token, as its more or less the setup of the test.
You don't want to try and define orders in tests, most testing frameworks have concepts around starting up a set of tests.
Example/docs https://xunit.net/docs/shared-context
https://learn.microsoft.com/en-us/visualstudio/test/using-microsoft-visualstudio-testtools-unittesting-members-in-unit-tests
but isn't this different in the case of integration tests than the docs you provided?
Because like i mentioned before, before i run a GET/UPDATE/DELETE test i would have to make sure the POST ran successfully else there is nothing to GET/UPDATE/DELETE.
should the POST be in a class fixture? but wouldn't it have to be tested before?
the post would be in the test fixture if you needed to handle the rest, it a test precondition
who cares if it's tested before?
The code in your test fixture doesn't need to tbe same code that runs your post in your app either.
thank you, that clears it up for me i think.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.