Unit testing in C# and .NET Core
Nowadays it is very important to pay attention to testing software applications. Despite this, unit tests are not usually discussed to support long-term and important projects. Throughout the project life cycle, it is absolutely necessary to maintain its functionality and integrity. Manually maintaining the integrity check of each new version is quite time-consuming and the human factor is not nullified. This also applies to applications that are developed on .NET Core.
What is unit testing
Unit testing is a programming process that allows you to check for correctness of individual modules of the program source code, put sets of one or more program modules together with the corresponding control data, and includes procedures for use and processing.
The idea is to write tests for each non-trivial function or method. This allows you to quickly check whether the most recent change in the code led to regression, that is, to the appearance of errors in already tested places of the program, and also facilitates the detection and elimination of such errors.
The purpose of unit testing is to isolate individual parts of the program and show that individually these parts are functional. This type of testing is usually done by programmers.
Testing small sections of code, i.e. "units"
To create unit tests, small sections of code are selected that need to be tested. The test site should generally be less than class. In most cases, a single class method or even part of the functions of a method is tested. The emphasis on small sections allows you to quickly write unpretentious tests.
Once written, code is often read multiple times, so it is important to write clear code. This is especially important in unit tests, where, in the case of failure during testing, the developer must quickly read the source code and understand what the problem is and how to fix it. The use of small sections of code greatly simplifies this work.
Testing in isolation from the rest of the code
When testing, it is important to isolate the code under test from the rest of the program with which it interacts, so that you can clearly identify the possibility of errors in this isolated code. This simplifies and enhances control over individual program components.
The creation of unit tests for small sections of code leads to the fact that the number of these unit tests becomes very large. If the process of obtaining results and conducting tests is not automated, this can lead to a waste of working time and lower productivity. Therefore, it is important that the results of unit tests be a simple solution; either the test is passed or not. To automate the process, developers usually turn to unit testing frameworks.
Testing only public endpoints
Even small changes in the class can lead to the failure of many unit tests because the implementation of the user class has changed. Therefore, when writing unit tests, they are limited to only public endpoints, which allows you to isolate unit tests from many details of the component's internal implementation. As a result, the probability that changes in classes can lead to failure of unit tests is reduced.
What is .NET Core
.NET Core is a modular open-source software development platform. It is compatible with operating systems such as Windows, Linux, and macOS. It was released by Microsoft.
.NET Core is based on the .NET Framework. The .NET Core platform differs from it in modularity, cross-platform, the ability to use cloud technologies, and in that there was a separation between the CoreFX library and the CoreCLR runtime.
.NET Core is a modular platform. Each of its components is updated through the NuGet package manager, which means that you can update its modules separately, while the .NET Framework is updated as a whole. Each application can work with different modules and does not depend on a single platform update.
As mentioned on xUnit.net is “a free, open-source, community-focused unit testing tool for the .NET Framework. Written by the original inventor of NUnit v2, xUnit.net is the latest technology for unit testing C#, F#, VB.NET, and other .NET languages. xUnit.net works with ReSharper, CodeRush, TestDriven.NET, and Xamarin. It is part of the .NET Foundation and operates under its code of conduct. It is licensed under Apache 2 (an OSI approved license).”
In general, xUnit is the collective name of a family of unit testing frameworks, the structure and functionality of which is based on SUnit, intended for the Smalltalk programming language. SUnit, developed by Kent Beck in 1998, was written in a highly structural object-oriented style, gained wide popularity, and has been adapted for many other languages. The names of the frameworks of this family are formed similarly to "SUnit", usually the letter "S" is replaced by the first letter (or the first few) in the name of the proposed language ("JUnit" for Java, "NUnit" for the .NET software platform, etc.). A family of such frameworks with a common architecture is commonly known as "xUnit".
In addition to xUnit.net, there are two more frameworks for testing. One of them is MS Test. This is a unit testing framework from Microsoft, which is included by default in Visual Studio and which can also be used with .NET Core. The second framework is NUnit, a ported framework with JUnit for the .NET platform.
How to make a unit test in C# .NET Core
In general, writing tests for C# .NET Core is similar to the same processes in C # for Windows and can be done in general scenarios.
As the most justified strategy for writing each individual test, for example, this may be:
- Arrange: Set up any variables and objects necessary.
- Act: Call the method being tested, passing any parameters needed
- Assert: Verify expected results
To indicate the methods that will be involved in testing, xUnit.net has constructions such as attributes. The most commonly used attributes are [Fact], [Theory], [InlineData (arg)]. Attributes allow you to mark the methods that will be run to perform the test, set input parameters for the tests, execute methods before and after testing, etc. In the following table, you can see what types of attributes you can use.
Assert in testing is a function or macro that checks the behavior or condition of the module under test. Often a statement is to verify the equality or inequality of some parameter of the module to the expected result. Failure to pass the test results in the failure of the entire test script and (if necessary) an exception that stops the script without moving on to the next statement.
A distinctive point in using unit tests in xUnit.net is that the SetUp section is described directly in the constructor (see the example below). A direct test takes place where the attribute [Fact] is specified. To run the test, you must also include the line “using Xunit;”. More details can be found at this link.
A very important feature in testing under xUnit.net is the ability to use the [Theory] attribute. This allows you to enter different values for the arguments to the test input. Thus, we save time and space for creating test function calls. This is very convenient and allows you to quickly test on many arguments, without changing the code to call the test.
In conclusion, it is worth noting that unit testing, especially in combination with Test Driven Development, gives tangible results in long-term and large projects, as well as in small projects. The developers of our company, have already had a lot of experience using unit tests, which gives them the capability to quickly and efficiently write tests with minimal additional costs. Of course, a full 100 percent test coverage in many cases would be pointless. But we have the experience to find the right ratio so that the tests are comprehensive without being redundant. In development environments such as .NET Core, there are very reliable tools for developing unit tests. Despite the cross-platform system, unit tests are well supported and you can equip projects with unit testing. Svitla Systems will complete your project as soon as possible with the required high level of quality, and unit tests are one method of ensuring success.
Let's discuss your project
We look forward to learning more and consulting you about your product idea or helping you find the right solution for an existing project.
Your message is received. Svitla's sales manager of your region will contact you to discuss how we could be helpful.