How to write test cases for SQl queries?
- is there any procedure or format to follow.
- for instance i am having an insert query with complex joins, and how to develop testcase for that.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That seems to be interesting, as I am not sure of your requirement.
IMHO, test cases are good to be written for functional or system testing (or Black box testing in general). Test cases are a translation of the requirements specs or the User stories. In case of testing a query, I am not sure if we need to write a test case (this becomes white box testing). In such a case it better to write Unit tests for them than creating manual test cases. May be I am missing something here.
However considering that you have a different opinion which I am not aware of, following is my idea.
If your User Story or the requirements spec explicitly talks about number and type of joins, tables to be used etc. then you can write test cases separately to check if there are any deviations. Eg. One test case can check for the type joins to check if they are as per the requirements and another test case can verify the tables that are used, to check if the developer has used only the intended tables and nothing else.
EDIT: In case you are willing to write unit tests, please check out DBUnit which helps you
For example, consider the following scenario. You have a DB of a school. You have now written a complex query which retrieves a list of students. You have to check if this query is working fine. Then you can create a unit test case using DBUnit or Unitils which will dynamically create your table and load your sample data, execute your query and then compare the resultset with the data which you expect. All in a single automated unit test.
This is an example from Unitils,
public class UserDAOTest extends UnitilsJUnit4 {
}
Before this test is executed, the sample data from this
"UserDAOTest.testFindByMinimalAge.xml"will be loaded into the table which you configure and later the test and the query inside it will be executed. Similarly if you use the annotation@ExpectedDataSetinstead of@DataSetthe data in this xml will be used for comparison of the result set after the test is executed.In case you are using JPA-Hibernate, you can also do effective unit testing by using an in-memory DB.
Hope this helps.