Priyanshu
Priyanshu

Reputation: 3058

Best way to test Rest APIs?

My organization developers have developed Rest APIs which exposes all the user details of application. We perform this testing on a particular testing environment and as of now I have developed a framework which works on top of selenium webdriver along with RestAssured.I'll explain what I have done in framework:

  1. Using selenium webdriver, frameowork creates all the prerequisite test data in application (for example test users, admin etc) as till now application does not provide POST APIs.

  2. Using RestAssured, It tests all the Rest API methods.

  3. To manage test data, framework uses a explicit SQLite based database.Selenium also uses same SQLite based data to create test data in building.

Problem which I have been facing:

  1. Management of test data is bit tricky here as application has too many things so I had to create multiple tables in SQLite and had to write very complex queries.

  2. API exposes users of application with sorting, pagination, filtering features so in case of any new data in application (which does not exist in SQLite database), it becomes very difficult to test actual response.

  3. There are too many problems but I'll try to solve it by myself.

For an instance if I want to test sorting feature then I sort it first by SQL queries and then comparing it with actual response. In case of new data the data which get sorted by sql query does not match with actual response, so it tends to fail test case while there is nothing wrong.

Any suggestion how I can a develop framework which will take care of creation of test data with api test.

Upvotes: 0

Views: 1347

Answers (1)

mansilladev
mansilladev

Reputation: 988

Priyanshu, I just want to make sure I'm understanding this. First, in lieu of WebDriver not supporting POST requests to your RESTful API to create your test data, you're making direct SQL queries. Second, when you're testing the sort feature through the UI using WebDriver, you're not getting matches against your tests which are also making direct SQL queries.

So, my question is: doesn't the interface of options (sorting, pagination, etc.) dictate what the REST GET query is going to be? Since you already have a REST API, I think that you might need to explore using another tool, rather than bridging the gap performing assertions against raw SQL queries and a browser-based testing tool that doesn't make any direct database connections (because it's just a web browser simulator).

If you want to test an API, use an API testing tool like Runscope - http://runscope.com. You can define a test with multiple API requests (e.g. one to POST new data using your REST API, another to GET data with any types of parameters/options around sorting/pagination, etc.) and simple assertions for data validation.

Upvotes: 1

Related Questions