vstest: 'dotnet test' in solution folder fails when non-test projects are in the solution
From @bungeemonkee on August 15, 2017 16:1
Description
Being able to run ‘dotnet test’ on a solution now is amazing! However, unless that solution contains ONLY test projects it always produces a failure result due to not finding the test sdk. In my experience it is rare for tests to be in a separate solution - they generally don’t exist or are in the same solution as the code they are testing.
It seems logical to me that when running tests on a solution any projects without the test sdk should be ignored. If a project has the test sdk and no tests or is being tested in isolation and does not have the sdk that is likely an error. However if a project does not have the sdk and is being tested as part of a solution that is likely not an error.
Alternatively, if that feels too much like a breaking change then adding an option to filter by patterns on the project name could suffice. Something like dotnet test --projects "*.Tests.*"
. Since most test projects in my experience have some form of pattern to the name - usually involving the word ‘test’ - this would probably be sufficient for most cases.
Note that the existing --filter <EXPRESSION>
switch is insufficient to prevent the exception, presumably because it is processed by the underlying test framework which happens after the missing sdk error is thrown.
Steps to reproduce
‘’’ dotnet new sln -n Solution dotnet new console -n Program dotnet sln Solution.sln add Program/Program.csproj dotnet new mstest -n Tests dotnet sln Solution.sln add Tests/Tests.csproj dotnet test $? ‘’’
Expected behavior
- Existing tests run.
- Projects with no test sdk reference are ignored.
- Return code is zero.
Actual behavior
- Exiting tests run.
- Projects with no test sdk reference are run as test projects and fail due to an error.
- Return code is non-zero. In this case it appears to be 1 but I’m not sure if that is consistent.
Environment data
dotnet --info
output:
.NET Command Line Tools (2.0.0)
Product Information: Version: 2.0.0 Commit SHA-1 hash: cdcd1928c9
Runtime Environment: OS Name: ubuntu OS Version: 16.04 OS Platform: Linux RID: ubuntu.16.04-x64 Base Path: /usr/share/dotnet/sdk/2.0.0/
Microsoft .NET Core Shared Framework Host
Version : 2.0.0 Build : e8b8861ac7faf042c87a5c2f9f2d04c98b69f28d
What is not clear form this environment output is that I am running in bash via the WSL.
Copied from original issue: dotnet/cli#7447
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 81
- Comments: 42 (5 by maintainers)
Commits related to this issue
- Workaround to fix issue 'https://github.com/Microsoft/vstest/issues/1129'. — committed to ob1dev/TechTasks by ob1dev 6 years ago
- Update test project and package references. NOTE: To test run `dotnet test ./Tests` due to this bug (https://github.com/Microsoft/vstest/issues/1129). — committed to jpierson/x-functional-helpers by jpierson 6 years ago
- Pickup some commits not related to Mono from #374 (#410) * Add some missing method to LoggingHandler * Avoid to alloc an huge error message when the test not failed. * Update the unittest * ... — committed to Soho-Rab/DotNetty by yyjdelete 6 years ago
- Implement fix for stderr spam about non-test projects when running `dotnet test` https://github.com/Microsoft/vstest/issues/1129#issuecomment-414484457 — committed to MeadowSuite/Meadow by zone117x 6 years ago
- Add MSBuild test targets Add MSBuild test targets to make the test runner not search for tests within non-test projects as per the following: https://github.com/Microsoft/vstest/issues/1129 https://... — committed to linked-data-dotnet/json-ld.net by asbjornu 6 years ago
The
dotnet pack
command looks for the<IsPackable>
property. It would be nice ifdotnet test
looked for<IsTestable>
. At face value, it seems like a really simple solution that would be quite valuable.fyi I wrote a blog post about my solution (enables
dotnet test
without needing to implicitly build projects multiple times) https://dasmulli.blog/2018/01/20/make-dotnet-test-work-on-solution-files/I’m currently using these msbuild targets in the solution directory to work around this:
A
Directory.Build.targets
file makes sure that all projects have aVSTestIfTTestProject
target:Then file named
after.MySolutionName.sln.targets
adds aVSTest
target to the solution itself thatdotnet test
will call:+1 to the requirement of ‘works out of the box’.
@livarcocc I ran into the same issue. There’s a quite good workaround for that: create a Directory.Build.targets in the root of your repository with the following content:
create the file OverrideTest.targets beside the Directory.Build.targets
what happens here is that the VSTest target is overridden as empty (nothing happens) when the project is not considered as test project by msbuild. A better solution (reusable across many repositories) is to put that into an build nuget package and just reference that package in your Directory.Build.targets, but unfortunately I can’t publish mine on nuget.org
@consultwithmike I don’t think it’s reasonable to expect every developer or organization to restructure all their solutions to work around this. Only testing projects that have some test sdk as a dependency - or supporting some project name filter - seems to me like a much more reasonable expectation.
@smadala Please pick this up for resolution as discussed.
While the current solution for @consultwithmike works, it might be convenient to run all your tests from a single command, to ease stat parsing. This can be achieved with
dotnet vstest
. However, you are required to specify every dll separately.For my project, this results in the following command (I know, the folders and casing are wrong, which is what you get when you maintain a .NET project originally created by Java developers):
dotnet vstest modules/**.tests/bin/Release/**/**.tests.dll
It wil run all test in the dll’s matching the argument selector, and give some final, accumulated output:
This method does not appear to be working from PowerShell.
The workaround I’m using right now is this script.
It simply strips out projects that don’t end with
.Tests
from a new copy of the solution file. To run the tests, do something along the lines of:--no-build
prevents build failures due to missing projects.HTH
In powershell I am using this:
Meaning “run all tests recursively in all sub-folders, where assemblies are named
*.Test.Unit.dll
and their paths contain\bin\Release\
”.Pretty sure it could be improved (by using proper directory filtering instead of string match, for instance), but it gets the job done.
I noticed that using dotnet 2.1.500 doing this
provides information that tests are skipped if they not include
IsTestProject = true
Was the issue already fixed with a release? I stumbled on this issue in a Build Pipeline on Azure DevOps using Linux Ubuntu 1604 host, running a .Net Core task
but the same was not happening using a Linux Preview Host, reporting the same task information.
I’d still prefer a solution that would treat it as a single test run. all solutions involve invoking vstest multiple times, either through sh commands or looking at all projects with
$(IsTestProject)
in the solution (see my blog post). But none them can:UPDATE
Even executing that command wasn’t working right so I had to build this bash script to do the job. Now I just execute the bash script.
The best way to solve this is to place all of your Tests project into their own folder, which is relatively good practice to keep large projects manageable. So, let’s assume a structure like this:
Now to run all tests you just use
dotnet test Tests/**
.It would be great if this would work out of the work!
@consultwithmike I’m glad you found a solution that works for you. I’m just making the point that the work-arounds are not viable options for everyone and that this is still an active issue for many of us.
To me it is working as expected on
2.1.500
! It looks for projects with<IsTestProject>true</IsTestProject>
in the solution.Finally, after more than 1 year waiting it works without workarounds! 🙂
For people who can’t make it work with Travis:
It started returning
code 0
when I changed my travis.yml file fromto
I know this could be a strange solution but I personally do the following.
The CLI would run tests only for test projects.
Now when new Test project is added only a solution configuration has to be modified (no CI changes involved).
Hope this helps.
@dasMulli Your method works, but it does result in several invocations of the test runner, leading to several output files and a longer execution time. It also does not generate an overview of all tests run in the end.
Due to this, I personally prefer the
dotnet vstest
solution as it results in a single output file, which runs faster and generates an overview at the end of execution.@jcdickinson That script worked really well. I was able to get my build process to run the tests without failing. Thanks!