Testing an ASP.NET MVC using XUnit-Part 1
Software testing is a process of executing a program or application with the intent of finding the software bugs.
It can also be described as the process of validating and verifying a software program or application or product.
The higher we go in the pyramid, the slower and more complex it becomes ,etc .
- Testing an Asp.net Core using XUnit -Introduction
- Testing an Asp.net Core using XUnit -Unit Test
- Testing an Asp.net Core using XUnit -Integration Test
- Testing an Asp.net Core using XUnit -Subcutaneous Test
- Testing an Asp.net Core Using XUnit -UI Test
The steps for using the unit test are as follows, which we will discuss in more detail in the following sections:
Tests should add value to the project.
The Testing Pyramid May not be applicable to every project.
Overview of the xUnit Tool
xUnit is a free, open-source, testing tool for .NET which developers use to write tests for their applications. It is essentially a testing framework which provides a set of attributes and methods we can use to write the test code for our applications. Some of those attributes, we are going to use are:
- [Fact] — attribute states that the method should be executed by the test runner
- [Theory] — attribute implies that we are going to send some parameters to our testing code. So, it is similar to the [Fact] attribute, because it states that the method should be executed by the test runner, but additionally implies that we are going to send parameters to the test method
- [InlineData] — attribute provides those parameters we are sending to the test method. If we are using the [Theory] attribute, we have to use the [InlineData] as well
As we said, xUnit provides us with a lot of assertion methods we use to validate our production code. As we progress through this series, we are going to use different assertion methods to test different production functionalities.
Quick Overview of the Starting Project
We can add XUnit with create new project and search XUnit like below picture :
Afrer choise XUnit and press next create a new test project like below :
Learning to use Test Explorer
Test Explorer is the name of the window that lets you browse and run your tests from within Visual Studio. Open it by choosing Test > Test Explorer
from the main menu. You should be greeted with a window that contains a hierarchy of the tests in your project, which should look something like this:
The toolbar of Test Explorer has buttons in several groups:
- The first group contains buttons that are used for running tests, including the ability to run all tests, run selected tests, re-run the last tests, and re-run only the failed tests.
- The second group contains buttons which allow filtering the list of tests based on current state (which includes “passed”, “failed”, and “not run”).
- The third group contains buttons which configure Test Explorer, including advanced options like changing processor architecture (x86, x64, or Auto) and automatically running tests after every successful build.
The main window of Test Explorer is split into two panes:
- The left pane contains a tree view of the tests in your project (grouping criteria can be changed as you see fit). Columns can be configured to show details about the test, including things like the current state, how long the test took to run, metadata (traits) related to the test, and more.
- As you select items in the tree view on the left pane, the right pane will provide information about your current selection, including test counts, outcomes, links to the source of the test, test output, and exception messages and stack traces for failed tests.
Click the left-most button on the Test Explorer toolbar (it looks like a double green arrow, titled “Run All Tests In View”. This will run the single empty test, and the result should be success:
Now that we’ve ensured everything is working, it’s time to write our first real tests.
In the next section we will talk about the unit test.