unit testing - Database data needed in integration tests; created by API calls or using imported data? -
This question is more or less programming language agnostic. However, as soon as I am in Java for most days, from there I will take my examples out of here. I am also thinking about the OOP case, so if you want to test a method, then you need an example of those method classes.
is that they should be autonomous, and it can be achieved by separating a class from its dependence. There are several ways to do this and it depends on whether you have your dependencies (in the world in Java we have Spring, EJB3 and other frameworks / platforms that provide injection capabilities) and / or if To separate the class of objects you have (for Java) and the class being tested with your dependency.
If we need to test the groups of methods in different sections and see that they are doing well, then we write . And this is my question!
- At least in web applications, the state often stays in the database. To achieve freedom from the database, we can use the same tool for unit tests. But in my humble opinion, I think there are cases where when you do not use the database for integration testing, you are joking too much (but feel free to disagree; never use the database, This is also a valid answer because it makes the question irrelevant).
- When you use the database for integration testing, how do you fill that database with the data? I can see two ways:
- Store database contents for integration testing and load it before starting the exam. If it is stored in the form of a SQL Dump, it would be interesting to know a database file, XML or something.
- Create the database structure needed by API calls. These calls are likely divided into several methods in your test code, and each of these methods can be
how are you doing this Does that require database data to be used when you need it? And why did you choose the method you selected?
Please answer with inspiration , because it is in inspiration, remember in the interesting part that just saying "this is the best practice!" An real is not inspiration, it has read or heard of someone by you, it has to be run again. Please tell why this is the best practice for that matter.
* In my definition the other methods of the same class (same or other) include one way of calling the unit test, even if it is not technically correct. Feel free to correct me, but keep it as a side issue.
I test data using API call
At the beginning of the trial, you create an empty database (in-memory or same which is used in the production), to start the installation script to run it, and then whatever test The data used by the database can be used for example test data, for example, so that the same data can be reused in many tests, possibly with minor variations.
You want that for every test, for reproductive tests without side effects, so when one exam ends, you should drop the test database or roll back the transaction, So that the test of the next test test can always be reproduced in the same way, even if the last test pass or fail.
The reason is that I would like to avoid importing database dumps (or similar), that is, the database will add test data with schema. When the database schema changes, you need to change or redo the test data, which may require manual work.
If the test data is specified in the code, then you have the power of your IDE's refactoring device. When you make a change in your hand that affects the database schema, it may be possible The APIs will affect calls, so that you need to use the code again to use the API. With almost the same effort, you can also refactor to build test data - especially if refactoring can be automated (rename, parameter, etc.). But if the database is dependent on the database dump, you will need to manually rebuild the database dump in addition to refactoring the code that uses the API.
Another thing related to database integration testing is that it is doing the right thing with the previous database schema, for this you may want to read the book or this article:
< / Div>
Comments
Post a Comment