Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing Integration tests to test database, web service calls

We are just starting to write integration tests to test database,data access layer, webservice calls etc. Currently I have some idea to write integration tests like 1) Always recreating tables in initialize function. 2) Always clear the data inside a function, if you are saving new inside the same function.

But I would like to know some more good practices.

like image 242
alice7 Avatar asked Nov 23 '11 17:11

alice7


People also ask

How do you write integration test cases for Restful web services?

Basics of Testing and Types There are three basic layers of testing: Unit test — making sure each function/unit of code works as expected. Functional test — making sure units interact with each other as expected. Integration test — making sure our app integrate with other app/api/services as expected.

Is database testing an integration test?

Integration tests focus on testing how separate parts of the program work together. In the context of applications using a database, integration tests usually require a database to be available and contain data that is convenient to the scenarios intended to be tested.

What do you write in an integration test?

Integration testing is a type of testing meant to check the combinations of different units, their interactions, the way subsystems unite into one common system, and code compliance with the requirements. For example, when we check login and sign up features in an e-commerce app, we view them as separate units.


3 Answers

As with all testing, it is imperative to start from a known state, and upon test completion, clear down to a clean state.

Also, test code often gets overlooked as not real code and hence not maintained properly... it is more important than code. At least as much design, should go into the architecture of your tests. Plan reasonable levels of abstraction, ie if you are testing a web app, consider having tiers like so: an abstraction of your browser interaction, an abstraction of your components on pages, an abstraction of pages and your tests. Tests interact with pages and components, pages interact with components, components interact with the browser interaction tier and the browser interaction tier interacts with your (possibly third party) browser automation library.

If your test code is not maintained properly or well-thought out, they become a hindrance more than an aid to writing new code.

If your team is new to testing, there are many coding katas out there that aim to teach the importance of good tests (and out of this comes good code), they generally focus on a unit testing level, however many of the principals are the same.

like image 140
Rich O'Kelly Avatar answered Sep 27 '22 13:09

Rich O'Kelly


In general, I would suggest you look into mocking your database access layer and web service call classes in order to make it more testable. There are lots of books on this subject, like Osherove's The Art of Unit Testing.

That having been said, integration tests should be repeatable. Thus, I would choose option 1, to write a script that can recreate the database from scratch with test data. Option 2 is more difficult, because it can hard to be sure that the cleaning function does not leave unwanted data residue.

like image 32
neontapir Avatar answered Sep 27 '22 13:09

neontapir


When unit testing the DAL, I do it like this:

[TestFixtureSetUp]
public void TestFixtureSetUp()
{

    //this grabs the data from the database using an XSD file to map the schema
    //and saves it as xml (backup.xml)
    DatabaseFixtureSetUp();  
}

[SetUp]
public void SetUp()
{

    //this inserts test data into the database from xml (testdata.xml)
    //it clears the tables first so you have fresh data before every test. 
    DatabaseSetUp();  
}

[TestFixtureTearDown]
public void TestFixtureTearDown()
{
     //this clears the table and inserts the backup data into the database
     //to return it to the state it was before running the tests.
    DatabaseFixtureTearDown();
}
like image 29
dolphy Avatar answered Sep 26 '22 13:09

dolphy