1

I'm somewhat new to Automated API Testing and have been experimenting with doing some API (functional?/regression?) tests in SoapUI and Katalon against multiple microservices (no-UI, just API), along with using assertions to check for a valid response. However, I'm not sure how to handle the state of the database before and after a test is run.

For example, if I upload something through a POST to an endpoint and it's supposed to create 5 entries, then I can assert that those 5 were created by calling another endpoint with a GET and checking whether 5 objects are returned in the array. However, if I run that same test case again then I'll receive 10 entries (since the old 5 entries still exist) but my old assertion will fail since it's expecting 5.

I guess I can try to use DELETE calls to manually clear the state before a run but then I have to first track down the ids of what I want to delete. Or I can have some sql that runs to make sure those records are deleted but I run into the same issue, and both options feel janky. The only other option seems to be to restore the databases from dump files but that limits one to only running such tests on a test environment, which I guess might be a good thing? However, I haven't seen such support in SoapUI or Katalon for restoring database so far.

What is the best way to handle the state of databases behind microservices while running API tests whether it's on a local machine or in the cloud?

1

You seem to have a REST design problem that is affecting the testability of the system.

Firstly, the POST could return the created entities (or references to it). Secondly, the GET could have a filter option, such as createdSince parameter.

Hiding these problems will make them accumulate until your system is barely testable.

Now, if you want to proceed without fixing these issues, SoapUI offers the possibility of running Groovy scripts which you can use to manipulate a database.

| improve this answer | |
0

You can set up a test listener that will run before the test case.

In Katalon Studio it will look something like this

class NewTestListener {
    /**
     * Executes before every test case starts.
     * @param testCaseContext related information of the executed test case.
     */
    @BeforeTestCase
    def sampleBeforeTestCase(TestCaseContext testCaseContext) {
        // Here you can send a GET request and note the number of records
    }

}

Than in the test case you compare the number of newly created records with the number frome the @BeforeTestCase hook.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.