Académique Documents
Professionnel Documents
Culture Documents
net
(desktop)
In this article, we will demonstrate getting started with xUnit.net, showing you how to write and
run your first set of unit tests.
Note: The examples were done with xUnit.net v2.1. The version numbers and paths may differ for
you, depending on which version you're using.
Now in the NuGet package manager window, take the following steps:
namespace MyFirstUnitTests
{
public class Class1
{
[Fact]
public void PassingTest()
{
Assert.Equal(4, Add(2, 2));
}
[Fact]
public void FailingTest()
{
Assert.Equal(5, Add(2, 2));
}
Build the solution to ensure that the code compiles. Now that you've written the first test, we
need a way to run it. Let's install the NuGet package with the console runner.
To run the console runner, use a command like the one hightlighted below. You should see
output similar to this:
> packages\xunit.runner.console.2.1.0\tools\xunit.console MyFirstUnitTests\bin\D
ebug\MyFirstUnitTests.dll
xUnit.net console test runner (64-bit .NET 4.0.30319.34014)
Copyright (C) 2016 .NET Foundation.
Starting: MyFirstUnitTests.dll
MyFirstUnitTests.Class1.FailingTest [FAIL]
Assert.Equal() Failure
Expected: 5
Actual: 4
Stack Trace:
MyFirstUnitTests\Class1.cs(16,0): at MyFirstUnitTests.Class1.FailingTes
t()
Finished: MyFirstUnitTests.dll
Note: your path names my vary, depending on what name you chose for your project and which
version of xUnit.net you installed.
The console runner has several command line options, which include options for parallelization, test
filtering, and result reporting. To learn more about the console runner options, run the console
runner with no command line options.
Now that we've gotten your first unit tests to run, let's introduce one more way to write tests:
using theories.
Facts are tests which are always true. They test invariant conditions.
Theories are tests which are only true for a particular set of data.
A good example of this testing numeric algorithms. Let's say you want to test an algorithm
which determines whether a number is odd or not. If you're writing the positive-side tests (odd
numbers), then feeding even numbers into the test would cause it fail, and not because the test
or algorithm is wrong.
Let's add a theory to our existing facts (including a bit of bad data, so we can see it fail):
[Theory]
[InlineData(3)]
[InlineData(5)]
[InlineData(6)]
public void MyFirstTheory(int value)
{
Assert.True(IsOdd(value));
}
This time when we compile and run our tests, we see a second failure, for our theory that was
given 6:
Starting: MyFirstUnitTests.dll
MyFirstUnitTests.Class1.FailingTest [FAIL]
Assert.Equal() Failure
Expected: 5
Actual: 4
Stack Trace:
MyFirstUnitTests\Class1.cs(16,0): at MyFirstUnitTests.Class1.FailingTes
t()
MyFirstUnitTests.Class1.MyFirstTheory(value: 6) [FAIL]
Assert.True() Failure
Stack Trace:
MyFirstUnitTests\Class1.cs(30,0): at MyFirstUnitTests.Class1.MyFirstThe
ory(Int32 value)
Finished: MyFirstUnitTests.dll
If you're having problems discovering or running tests, you may be a victim of a corrupted
runner cache inside Visual Studio. To clear this cache, shut down all instances of Visual
Studio, then delete the folder %TEMP%\VisualStudioTestExplorerExtensions . Also make
sure your project is only linked against a single version of the Visual Studio runner NuGet
package ( xunit.runner.visualstudio ).
If you have Visual Studio Community (or a paid-for version of Visual Studio), you can run your
xUnit.net tests within Visual Studio's built-in test runner (named Test Explorer). Unfortunately,
this does not include Express editions of Visual Studio (you should upgrade to the free
Community Edition (http://www.visualstudio.com/en-us/news/vs2013-community-vs.aspx)
instead).
Right click on the project in Solution Explorer and choose Manage NuGet Packages . Search for
(and install) a package named xunit.runner.visualstudio :
Make sure Test Explorer is visible (go to Test > Windows > Test Explorer ). Every time you
build your project, the runner will discover unit tests in your project. After a moment of
discovery, you should see the list of discovered tests:
Click the Run All link in the Test Explorer window, and you should see the results update in
the Test Explorer window as the tests are run:
You can click on a failed test to see the failure message, and the stack trace. You can click on
the stack trace lines to take you directly to the failing line of code.
If you have Visual Studio Ultimate and you have the CodeLens feature turned on, your source
code will now show icons and text related to your tests:
CodeLens not only shows you the last run status of your unit tests, it also offers a convenient
place to click to run an individual test.
Note: Only xUnit.net v2 supports pre-enumeration of theories; when discovering theories with v1, it
will only show a single test method for the theory.
Note: CodeLens does not support icons for pre-enumerated theories; this is a limitation of the Visual
Studio test runner. In a future release, we will enable the ability to turn off theory pre-enumeration of
theory data for users who would prefer to have the CodeLens icons rather than individually runnable
tests.
Return Meaning
code
The help page was shown, either because it was requested, or because the
2
user did not provide any command line arguments.
There was a problem with one of the command line options passed to the
3
runner.
There was a problem loading one or more of the test assemblies (for
4
example, if a 64-bit only assembly is run with the 32-bit test runner).