liveBook · Manning

Sequential test

As explained in Unit Testing Principles, Practices, and Patterns

Running tests one after another so that only one test executes at a time.

Sequential testing runs integration tests one after another, keeping only one test active at a time. It reduces interference when tests share a database and makes leftover-data cleanup more manageable. section 10.3

Use sequential execution when:

  • Tests share one database instance and could violate constraints or consume each other’s data when parallelized. section 10.3
  • The performance gain from parallel execution does not justify unique test data, more complicated cleanup, and additional maintenance. section 10.3
  • Keep test state independent by clearing leftover data and having each test establish its required database state. section 10.3
Listing 10.6. Base class for integration tests
public abstract class IntegrationTests
{
    private const string ConnectionString = "...";

    protected IntegrationTests()
    {
        ClearDatabase();
    }

    private void ClearDatabase()
    {
        string query =
            "DELETE FROM dbo.[User];" +    #1
            "DELETE FROM dbo.Company;";    #1

        using (var connection = new SqlConnection(ConnectionString))
        {
            var command = new SqlCommand(query, connection)
            {
                CommandType = CommandType.Text
            };

            connection.Open();
            command.ExecuteNonQuery();
        }
    }
}

A base integration-test class clears database tables before each test, providing a concrete sequential-execution setup. section 10.3

How Unit Testing Principles, Practices, and Patterns covers it

How deep it goes
Compares execution strategies and maintenance costs, but defers detailed cleanup alternatives beyond the supplied discussion.
Examples
Uses a shared database, a base integration-test class, table-clearing SQL, and per-test Docker containers as contrasting examples.
What it assumes
Assumes integration tests, shared databases, and test collections, while emphasizing state setup and cleanup.
What you can do after
Operate/configure integration-test collections so database-backed tests run sequentially and clear shared state between runs.
How it connects
Connects integration testing with test isolation, database cleanup, parallel test execution, and containers.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Unit Testing Principles, Practices, and Patterns. The section links point to the parts of the book it draws on.