Integration Testing best practices

后端 未结 5 400
隐瞒了意图╮
隐瞒了意图╮ 2020-12-24 03:42

Our team has hundreds of integration tests that hit a database and verify results. I\'ve got two base classes for all the integration tests, one for retrieve-only tests and

相关标签:
5条回答
  • 2020-12-24 04:07

    Keep your fast (unit) and slow (integration) tests separate, so that you can run them separately. Use whatever method for grouping/categorizing the tests is provided by your testing framework. If the testing framework does not support grouping the tests, move the integration tests into a separate module that has only integration tests.

    The fast tests should take only some seconds to run all of them and should have high code coverage. These kind of tests allow the developers to refactor ruthlessly, because they can do a small change and run all the tests and be very confident that the change did not break anything.

    The slow tests can take many minutes to run and they will make sure that the individual components work together right. When the developers do changes that might possibly break something which is tested by the integration tests but not the unit tests, they should run those integration tests before committing. Otherwise, the slow tests are run by the CI server.

    0 讨论(0)
  • 2020-12-24 04:23

    We have an SQL Server Express instance with the same DB definition running for every dev machine as part of the dev environment. With Windows authentication the connection strings are stable - no username/password in the string.

    What we would really like to do, but haven't yet, is see if we can get our system to run on SQL Server Compact Edition, which is like SQLite with SQL Server's engine. Then we could run them in-memory, and possibly in parallel as well (with multiple processes).

    0 讨论(0)
  • 2020-12-24 04:26

    Have you done any measurements (using timers or similar) to determine where the tests spend most of their time?

    If you already know that the database recreation is why they're time consuming a different approach would be to regenerate the database once and use transactions to preserve the state between tests. Each CUD-type test starts a transaction in setup and performs a rollback in teardown. This can significantly reduce the time spent on database setup for each test since a transaction rollback is cheaper than a full database recreation.

    0 讨论(0)
  • 2020-12-24 04:27

    in NUnit you can decorate your test classes (or methods) with an attribute eg:

    [Category("Integration")]
    public class SomeTestFixture{
        ...
    }
    [Category("Unit")]
    public class SomeOtherTestFixture{
        ...
    }
    

    You can then stipulate in the build process on the server that all categories get run and just require that your developers run a subset of the available test categories. What categories they are required to run would depend on things you will understand better than I will. But the gist is that they are able to test at the unit level and the server handles the integration tests.

    0 讨论(0)
  • 2020-12-24 04:31

    I'm a java developer but have dealt with a similar problem. I found that running a local database instance works well because of the speed (no data to send over the network) and because this way you don't have contention on your integration test database.

    The general approach we use to solving this problem is to set up the build scripts to read the database connection strings from a configuration file, and then set up one file per environment. For example, one file for WORKSTATION, another for CI. Then you set up the build scripts to read the config file based on the specified environment. So builds running on a developer workstation run using the WORKSTATION configuration, and builds running in the CI environment use the CI settings.

    It also helps tremendously if the entire database schema can be created from a single script, so each developer can quickly set up a local database for testing. You can even extend this concept to the next level and add the database setup script to the build process, so the entire database setup can be scripted to keep up with changes in the database schema.

    0 讨论(0)
提交回复
热议问题