Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core 1.0 - How to run "All tests in Solution" with xUnit command line

The Getting started with xUnit.net (.NET Core / ASP.NET Core) page describes how to run tests with dotnet test command line.

It states that it requires a specific project.json, where we add xunit dependencies and test runner:

  "testRunner": "xunit",
    "dependencies": {
        "xunit": "2.1.0",
        "dotnet-test-xunit": "1.0.0-rc2-build10015"
    }

If I try calling it from the parent directory:

C:\git\Project\test [master ≡]> dotnet test
dotnet-test Error: 0 : System.InvalidOperationException: C:\git\Project\test\project.json does not exist.
   at Microsoft.DotNet.Tools.Test.TestCommand.GetProjectPath(String projectPath)
   at Microsoft.DotNet.Tools.Test.TestCommand.DoRun(String[] args)
C:\git\Project\test [master ≡]>

Question: Is there a way to run all tests (multiple project.json) with a single dotnet test?

like image 295
Bruno Garcia Avatar asked May 19 '16 12:05

Bruno Garcia


People also ask

How do you write xUnit test cases in .net core?

To write a test you simply create a public method that returns nothing and then decorate it with the Fact attribute. Inside that method you can put whatever code you want but, typically, you'll create some object, do something with it and then check to see if you got the right result using a method on the Assert class.

What test runners can be used to test xUnit.net tests?

The MSBuild runner in xUnit.net v2 is capable of running unit tests from both xUnit.net v1 and v2. It can run multiple assemblies at the same time, and build file options can be used to configuration the parallelism options used when running the tests.


2 Answers

In case anyone looks for a Windows answer, here's oneliner in PowerShell that does the job:

dir test | % { dotnet test $_.FullName }

like image 114
Andrzej Lichnerowicz Avatar answered Oct 08 '22 02:10

Andrzej Lichnerowicz


Since it's been almost a month and no answer, I'll at least share what I've been doing. (this won't be relevant once Visual Studio "15" RTM is launched because project.json is dead)

Simply using a for loop on all project.json:

Locally, from the test directory, I just run:

for /f %a in ('dir /b /s project.json ^| find /v "TestUtilities"') do dotnet test %a

Running it on all project.json except where the path has: TestUtilities

Mind that on TeamCity you need to escape % (and in scripts you need double: %%) so it goes by:

for /f %%%a in ('dir /b /s project.json ^| find /v "TestUtilities"') do dotnet test %%%a

Note the %%%. Since % in TeamCity is used for variables, the third % escapes it.

like image 20
Bruno Garcia Avatar answered Oct 08 '22 02:10

Bruno Garcia