Unit tests are used to aid in the development of Sports Store. We use the MSTest features that exists in Visual Studio for this purpose. Unit tests typically set up test data, call a programmatic entity, and validate whether it operates as intended.
There are three types of tests. Tests that do not hit infrastructure such as databases are run as part of the continuous integration (CI) build.
Integration tests and automated QA tests take longer to run and are not included in CI builds. These tests are covered in future articles.
Above, we have a unit test that tests the MVC category-selecting portion of Sports Store. These tests are “mocked,” so they do not hit the database.
TestCategory is used to identify tests that will be run as part of a CI build, so it is set to CI_Build.
The arrange code sets up the data for the test. The act code invokes the codebase we wish to test. The assert code validates our test.
We can see that our code passes the test.
This line will break the test because Category will now always have “zz” as a suffix.
Test Explorer in Visual Studio now shows that the test has failed.
It is best practice for the development team to run all unit tests before committing any changes to version control and performing a pull request.
Even if the development team does not follow this practice, the code review should catch the failed test. Let’s see what happens if this change makes it into the sprint branch when these best practices are not followed.
Three Tests now failed.
We can see the details of the failed unit test. This helps us determine what went wrong.
This demonstrates a key aspect of DevOps. We want to know about errors as early as possible before the codebase hits production.
The CI build is run every time a pull request is processed. This happens many times during a typical sprint, so we have many opportunities to find errors.
Next, we will discuss how to deploy Sports Store to various environments using releases.
For details on the topics covered in this article, please visit the links below. You may also download the source code for the DevOps Roadmap Lab on Git Hub. Contact me to set up a free meeting where we can dive deeply into the lab material and discuss your individual needs.
- Read about getting started with unit testing.
- One way to use unit tests is to follow the test-driven development process. A short article on this topic by Martin Fowler is available here.
- This article on TDD also gives solid unit test advice.