Unit testing - Rades98/AlzaCaseStudy GitHub Wiki

What is it

Unit testing is a software development process in which the smallest testable parts of an application, called units, are individually and independently scrutinized for proper operation. This testing methodology is done during the development process by the software developers and sometimes QA staff. The main objective of unit testing is to isolate written code to test and determine if it works as intended.

Unit testing is an important step in the development process, because if done correctly, it can help detect early flaws in code which may be more difficult to find in later testing stages.

Unit testing is a component of test-driven development (TDD), a pragmatic methodology that takes a meticulous approach to building a product by means of continual testing and revision. This testing method is also the first level of software testing, which is performed before other testing methods such as integration testing. Unit tests are typically isolated to ensure a unit does not rely on any external code or functions. Testing can be done manually but is often automated.

How does it work

A unit test typically comprises of three stages: plan, cases and scripting and the unit test itself. In the first step, the unit test is prepared and reviewed. The next step is for the test cases and scripts to be made, then the code is tested.

Test-driven development requires that developers first write failing unit tests. Then they write code and refactor the application until the test passes. TDD typically results in an explicit and predictable code base.

Each test case is tested independently in an isolated environment, as to ensure a lack of dependencies in the code. The software developer should code criteria to verify each test case, and a testing framework can be used to report any failed tests. Developers should not make a test for every line of code, as this may take up too much time. Developers should then create tests focusing on code which could affect the behavior of the software being developed.

Unit testing involves only those characteristics that are vital to the performance of the unit under test. This encourages developers to modify the source code without immediate concerns about how such changes might affect the functioning of other units or the program as a whole. Once all of the units in a program have been found to be working in the most efficient and error-free manner possible, larger components of the program can be evaluated by means of integration testing. Unit tests should be performed frequently, and can be done manually or can be automated.

Advantages and disadvantages of unit testing

Advantages to unit testing include:

The earlier a problem is identified, the fewer compound errors occur. Costs of fixing a problem early can quickly outweigh the cost of fixing it later. Debugging processes are made easier. Developers can quickly make changes to the code base. Developers can also re-use code, migrating it to new projects.

Disadvantages include:

Tests will not uncover every bug. Unit tests only test sets of data and its functionality—it will not catch errors in integration. More lines of test code may need to be written to test one line of code—creating a potential time investment.

Shouldly

Shouldly is an open source library that has its home on GitHub and is easily installable via NuGet.

It doesn't replace the existing testing framework so, for example, it would be installed into your existing NUnit or xUnit.net test project. Once the NuGet package is installed and the relevant Shouldly using statement is added, asserts can now be written in the Shouldly style.

Shouldly does not have to replace all of the asserts in an existing test project, they can be introduced gradually into existing test suites and can live alongside other tests that use the built-in assert API from the underlying testing framework.

Installation

Shouldly can be found here on NuGet and can be installed by copying and pasting the following command into your Package Manager Console within Visual Studio (Tools > NuGet Package Manager > Package Manager Console).
Install-Package Shouldly

Pros

  • Disambiguates expected and actual values
   Assert.Equal(result, 3); OR Assert.Equal(3, result); vs result.ShouldBe(3); 
  • Produces fluently readable code Assert.NotEmpty() Failure vs Shouldly.ShouldAssertException : model should not be empty but was

Custom extensions

Shouldly extension methods class should be provided with [ShouldlyMethods] attribute, as can be seen in Shouldly repo, final error messages are provided the same way as normally.

⚠️ **GitHub.com Fallback** ⚠️