Unit Tests

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.

Sports Store uses Moq as a mocking framework and Ninject for dependency injection.

Unit Test Code.png

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.

UnitTestPass.png

We can see that our code passes the test.

BreakUnitTest.png

This line will break the test because Category will now always have “zz” as a suffix.

UnitTestFailsVS.png

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.

3TestsFail.png

Three Tests now failed.

TestFailDetail.png

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.


Learn More

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 HubContact me to set up a free meeting where we can dive deeply into the lab material  and discuss your individual needs.