This article is a mirror article of machine translation, please click here to jump to the original article.

View: 31285|Reply: 3

[Source] .net/c# uses xunit unit tests

[Copy link]
Posted on 1/22/2019 2:58:26 PM | | | |
Currently in. The testing tools under the Net framework mainly include Nunit, built-in MSTest, and Xunit, and this article will briefly introduce how to use XUnit, a rising star in VS.

The minimum runtime supported by xunit is . NET 4.5.2, most of which are higher. Even if you can build a 4.0 class library, it will still work on at least .NET 4.5.2. Detailed links:https://github.com/xunit/xunit/issues/1817


First, I created a new .NET 4.5.2 console project with the name: itsvse-test, and the latest version of xunit is: 2.4.1, so the nuget command is as follows:

Each package is licensed to you by its owner. NuGet is not responsible for third-party packages and does not license them. Some packages may include dependencies that are subject to other licenses. Click on the package source (source) URL to determine any dependencies.

Package Manager console host version 4.7.0.5212

Type "get-help NuGet" to see all available NuGet commands.

PM> Install-Package xunit -Version 2.4.1


Attempts are being made to collect and target ". NETFramework, Version=v4.5.2" of the project "itsvse-test" about the package "xunit.2.4.1"
It takes 2.1 sec to collect dependency information
Trying to resolve the dependency of package "xunit.2.4.1" with DependencyBehavior as "Lowest"
Parsing dependency information takes 0 ms
Parsing operation to install package "xunit.2.4.1"
Resolved operation to install package "xunit.2.4.1"
Retrieve package "xunit 2.4.1" from "cnblogs"
Retrieve package "xunit.abstractions 2.0.3" from "cnblogs"
Retrieve package "xunit.analyzers 0.10.0" from "cnblogs"
Retrieve package "xunit.assert 2.4.1" from "cnblogs"
Retrieve package "xunit.core 2.4.1" from "cnblogs"
Retrieve package "xunit.extensibility.core 2.4.1" from "cnblogs"
Retrieve package "xunit.extensibility.execution 2.4.1" from "cnblogs"
  GEThttps://api.nuget.org/v3-flatcon ... 1/xunit.2.4.1.nupkg
  GEThttps://api.nuget.org/v3-flatcon ... actions.2.0.3.nupkg
  GEThttps://api.nuget.org/v3-flatcon ... it.core.2.4.1.nupkg
  GEThttps://api.nuget.org/v3-flatcon ... .assert.2.4.1.nupkg
  GEThttps://api.nuget.org/v3-flatcon ... ty.core.2.4.1.nupkg
  GEThttps://api.nuget.org/v3-flatcon ... ecution.2.4.1.nupkg
  GEThttps://api.nuget.org/v3-flatcon ... lyzers.0.10.0.nupkg
  OKhttps://api.nuget.org/v3-flatcon ... actions.2.0.3.nupkg116 ms
  OKhttps://api.nuget.org/v3-flatcon ... 1/xunit.2.4.1.nupkg117 ms
Installing xunit.abstractions 2.0.3.
Installing xunit 2.4.1.
  OKhttps://api.nuget.org/v3-flatcon ... it.core.2.4.1.nupkg151 ms
Installing xunit.core 2.4.1.
  OKhttps://api.nuget.org/v3-flatcon ... .assert.2.4.1.nupkg167 ms
Installing xunit.assert 2.4.1.
  OKhttps://api.nuget.org/v3-flatcon ... ty.core.2.4.1.nupkg221 ms
Installing xunit.extensibility.core 2.4.1.
  OKhttps://api.nuget.org/v3-flatcon ... lyzers.0.10.0.nupkg200 ms
Installing xunit.analyzers 0.10.0.
  OKhttps://api.nuget.org/v3-flatcon ... ecution.2.4.1.nupkg306 ms
Installing xunit.extensibility.execution 2.4.1.
Adding package "xunit.abstractions.2.0.3" to folder "C:\Users\itsvse_pc\Source\Repos\itsvse-test\packages"
Package "xunit.abstractions.2.0.3" has been added to folder "C:\Users\itsvse_pc\Source\Repos\itsvse-test\packages"
Added package "xunit.abstractions.2.0.3" to "packages.config"
Successfully installed "xunit.abstractions 2.0.3" to itsvse-test
Adding toolset only package "xunit.analyzers.0.10.0" to "itsvse-test"
Adding package "xunit.analyzers.0.10.0" to folder "C:\Users\itsvse_pc\Source\Repos\itsvse-test\packages"
Package "xunit.analyzers.0.10.0" has been added to the folder "C:\Users\itsvse_pc\Source\Repos\itsvse-test\packages"
Added package "xunit.analyzers.0.10.0" to "packages.config"
Script file "C:\Users\itsvse_pc\Source\Repos\itsvse-test\packages\xunit.analyzers.0.10.0\tools\install.ps1"
"xunit.analyzers 0.10.0" has been successfully installed to itsvse-test
Adding package "xunit.assert.2.4.1" to folder "C:\Users\itsvse_pc\Source\Repos\itsvse-test\packages"
Package "xunit.assert.2.4.1" has been added to the folder "C:\Users\itsvse_pc\Source\Repos\itsvse-test\packages"
Added package "xunit.assert.2.4.1" to "packages.config"
"xunit.assert 2.4.1" has been successfully installed to itsvse-test
Adding package "xunit.extensibility.core.2.4.1" to folder "C:\Users\itsvse_pc\Source\Repos\itsvse-test\packages"
Package "xunit.extensibility.core.2.4.1" has been added to the folder "C:\Users\itsvse_pc\Source\Repos\itsvse-test\packages"
Added package "xunit.extensibility.core.2.4.1" to "packages.config"
"xunit.extensibility.core 2.4.1" has been successfully installed to itsvse-test
Adding package "xunit.extensibility.execution.2.4.1" to folder "C:\Users\itsvse_pc\Source\Repos\itsvse-test\packages"
Package "xunit.extensibility.execution.2.4.1" has been added to the folder "C:\Users\itsvse_pc\Source\Repos\itsvse-test\packages"
Added package "xunit.extensibility.execution.2.4.1" to "packages.config"
"xunit.extensibility.execution 2.4.1" has been successfully installed to itsvse-test
Adding package "xunit.core.2.4.1" to folder "C:\Users\itsvse_pc\Source\Repos\itsvse-test\packages"
Package "xunit.core.2.4.1" has been added to the folder "C:\Users\itsvse_pc\Source\Repos\itsvse-test\packages"
Added package "xunit.core.2.4.1" to "packages.config"
"xunit.core 2.4.1" has been successfully installed to itsvse-test
Adding package "xunit.2.4.1" with only dependencies to project "itsvse-test".
Package "xunit.2.4.1" is being added to the folder "C:\Users\itsvse_pc\Source\Repos\itsvse-test\packages"
Package "xunit.2.4.1" has been added to the folder "C:\Users\itsvse_pc\Source\Repos\itsvse-test\packages"
Package "xunit.2.4.1" has been added to "packages.config"
"xunit 2.4.1" has been successfully installed to itsvse-test
Nuget took 5.51 sec to execute
Time Elapsed: 00:00:07.7781544
PM>
Install the plug-in packages: xunit.runner.console (omitted in this article) and xunit.runner.visualstudio

where xUnit is the framework,And xunit.runner.visualstudio is the VS plugin package, so that we can easily debug in VS.Installing xunit.runner.console can also be debugged under CMDBut if only this is supported, I believe not many people will use this framework, after all, the way of typing commands seriously lowers the grade of Visual Studio, so I will not explain it here.

Here, we only install the xunit.runner.visualstudio plugin package, right-click on the project in Solution Explorer and select Manage NuGet Packages. Search for (and install) a package named xunit.runner.visualstudio:



Make sure the test browser is visible (go to the Test > window > Test Explorer). Every time a project is built, the runner discovers unit tests in the project. After a period of discovery, you should see a list of discovered tests:



Edit the Program file and change the following code:

Click the link in the Run All window and you should see the result update in the Test Explorer window when you run the test:



You can click on the failed test to view the failure message and stack trace. You can click on the stack trace line to go directly to the failed line of code.

(End)





Previous:Sleep Your Sister Alarm Clock v4.6.1 Professional Paid Premium Chinese Version
Next:Today's C# Study Notes
 Landlord| Posted on 1/22/2019 3:19:24 PM |
Xunit does not need to tag test classes

xUnit supports both [Fact] and [Theory] properties. As you can see from the code, [Fact] is similar to the actual call code we write, while [Theory] with InlineData can test multiple sets of parameters directly in one method.

 Landlord| Posted on 1/22/2019 3:31:55 PM |
Anomaly testing

Xunit does not flag exception catches via Attributes, but directly uses the Assert.Throws assertion function to validate exceptions.

    public class TestClass1
    {
        [Fact]
        public void testException()
        {
            Assert.Throws<InvalidOperationException>(() => operation());
        }

        void operation()
        {
            throw new InvalidOperationException();
        }
    }



Change the test case name:

     [Fact(DisplayName = "Max Function Test")]



Skip test cases:

     [fact(skip="refactoring incomplete")]



Grouping:

     [Trait("Group", "Category")]
 Landlord| Posted on 1/22/2019 3:37:28 PM |
In many cases, we don't want unit tests to affect the content in the database, and sometimes our unit tests will be very slow due to the impact of the database, so we often want to isolate the persistence part, and do not really persist the data when doing unit tests. This kind of isolation We generally use abstraction, that is, use interfaces or abstract classes to isolate the persistence layer, and then use mocks to simulate the corresponding interfaces or abstract classes to complete the corresponding persistence classes. MoQ is one of these Mock frameworks, MoQ uses C# 3.0, which is simpler to use than NMock, and it is a strongly typed way, and the source code and dll can be reachedThe hyperlink login is visible.Download. The latest release of MoQ is now version 3.1, and 4.0 is still in beta.
https://www.cnblogs.com/catcher1994/p/5498530.html
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com