vstest: Unable to run/debug XUnit tests targeting Desktop from Visual Studio on latest Windows 10 machine
Description
After the Windows 10 Creators update, I can no longer run or debug xunit tests targeting Desktop .NET Framework in Visual Studio.
Steps to reproduce
-
On a machine with .NET 4.7 and VS 2017 15.2. I think .NET 4.7 comes with Windows Creators update. After I installed .NET 4.7 on another machine that had .NET 4.6, I don’t see
System.Runtime, Version=4.1.0.0
in GAC. The version 4.0.0.0 is in GAC. -
Create an xunit test project with
dotnet new xunit
. -
In the generated .csproj file, change the target framework to
net46
. Also change the package reference versions.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net46</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
</ItemGroup>
</Project>
- Open the project in Visual Studio, rebuild the project, then look at the Test output pane
Expected:
No warnings or errors
Actual:
there’s a warning
[xUnit.net 00:00:00.0226725] Skipping: test4 (could not find dependent assembly 'Microsoft.Extensions.DependencyModel, Version=1.1.0')
- Right click the test in Test Explorer then select Run Selected Tests
Expected:
Test runs and passes.
Actual:
------ Run test started ------
[xUnit.net 00:00:00.0296561] test4: Catastrophic failure: System.TypeInitializationException: The type initializer for 'Xunit.DiaSession' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Reflection.TypeExtensions, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
at Xunit.DiaSession..cctor()
--- End of inner exception stack trace ---
at Xunit.DiaSession..ctor(String assemblyFileName)
at Xunit.DiaSessionWrapper..ctor(String assemblyFilename)
at Xunit.XunitFrontController..ctor(AppDomainSupport appDomainSupport, String assemblyFileName, String configFileName, Boolean shadowCopy, String shadowCopyFolder, ISourceInformationProvider sourceInformationProvider, IMessageSink diagnosticMessageSink)
at Xunit.Runner.VisualStudio.TestAdapter.VsTestRunner.RunTestsInAssembly(IRunContext runContext, IFrameworkHandle frameworkHandle, LoggerHelper logger, IMessageSinkWithTypes reporterMessageHandler, AssemblyRunInfo runInfo)
========== Run test finished: 0 run (0:00:00.427418) ==========
Environment
Operating system: Windows 10: a Microsoft internal selfhost version)
vstest.executionengine.x86.exe: 15.00.26228.0
My findings
-
I am using the SDK-style .csproj to target net46. According to the doc, Desktop, UWP & Native unit testing continues to use the test platform (TPv1) located @ “%programfiles(x86)%\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TestWindow”.. (I have briefly tried the method of putting a
testplatform.config
mentioned at the same link but it didn’t work for me.) -
I have .NET 4.7 on my repro machine. I suspect that
System.Runtime, Version=4.1.0.0
is in the GAC for latest windows builds. -
When discovering/executing unit tests, VS tries to get additional test extensions from both xunit assemblies in the following order
%TEMP%\VisualStudioTestExplorerExtensions\2.2.0\build\netcoreapp1.0\xunit.runner.visualstudio.dotnetcore.testadapter.dll
%Temp%\VisualStudioTestExplorerExtensions\2.2.0\build\_common\xunit.runner.visualstudio.testadapter.dll
-
Because of the GAC version of
System.Runtime.dll, Version=4.1.0.0
, xunit test extension from the first assembly (.NET Core version) is successfully added. -
The test extension from the second assembly (Desktop version) is also added.
-
Now VS test platform has two test adapters for
executor://xunit/VsTestRunner2
-
When I rebuild the solution, the VS Test discovery engine uses the first one (.NET Core version) and failed to load its dependency
Microsoft.Extensions.DependencyModel, Version=1.1.0.0
because my test is targeting net46. Note, I am not clear why this dependency didn’t cause problem when VS tries to get types via reflection from assemblyxunit.runner.visualstudio.dotnetcore.testadapter.dll
. It seems still able to find the test (using the second xunit test adapter?)
------ Discover test started ------
[xUnit.net 00:00:00.0042451] Skipping: test4 (could not find dependent assembly 'Microsoft.Extensions.DependencyModel, Version=1.1.0')
[xUnit.net 00:00:00.2620017] Discovering: test4
[xUnit.net 00:00:00.3980642] Discovered: test4
========== Discover test finished: 1 found (0:00:00.6219404) ==========
-
When I run a test from VS Test Explorer, it failed.
-
On my non-reproing machine that have .NET 4.6. VS also tries to get test extensions from the same two xunit assemblies. But getting types via reflection fails for the .NET Core version because of the missing dependency on
System.Runtime, Version=4.1.0.0
. TheReflectionTypeLoadException
is handled. Thus only the Desktop version of xunit test adapter is added to the list of test discoverers.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 24
- Comments: 32 (13 by maintainers)
Commits related to this issue
- Fix xUnit tests in VS2017 Due to an xUnit bug, tests would attempt to run, but then fail immediately. Updating the packages fixes the issue. https://github.com/Microsoft/vstest/issues/811 — committed to dustinsoftware/React.NET by dustinsoftware 7 years ago
- Fix xUnit tests in VS2017 Due to an xUnit bug, tests would attempt to run, but then fail immediately. Updating the packages fixes the issue. https://github.com/Microsoft/vstest/issues/811 — committed to dustinsoftware/React.NET by dustinsoftware 7 years ago
- Fix xUnit tests in VS2017 (#474) Due to an xUnit bug, tests would attempt to run, but then fail immediately. Updating the packages fixes the issue. https://github.com/Microsoft/vstest/issues/811 — committed to reactjs/React.NET by dustinsoftware 7 years ago
- Updated XUnit packages because current version was no longer working (https://github.com/Microsoft/vstest/issues/811). Removed HtmlAgilityPack (HAP) from the MalApi project. Added HAP to the integrati... — committed to naps250/mal-api by naps250 6 years ago
- Fix xUnit tests in VS2017 (#474) Due to an xUnit bug, tests would attempt to run, but then fail immediately. Updating the packages fixes the issue. https://github.com/Microsoft/vstest/issues/811 — committed to toptaldev92/React.NET by toptaldev92 7 years ago
- Fix xUnit tests in VS2017 (#474) Due to an xUnit bug, tests would attempt to run, but then fail immediately. Updating the packages fixes the issue. https://github.com/Microsoft/vstest/issues/811 — committed to onlinedev0808/React.NET by onlinedev0808 7 years ago
Workaround:
Why this is closed, the issue still persists
~@codito please re-open this as the issue is still reproing in 15.4~ I take it back. It seems to have been an issue with the cache. Once I deleted the folder
%TEMP%\VisualStudioTestExplorerExtensions
the problem went away.Restarting Visual Studio after deleting
AppData\Local\Temp\VisualStudioTestExplorerExtensions
did the trick for me.Try deleting your
AppData\Local\Temp\VisualStudioTestExplorerExtensions
folder and restarting visual studio.I also updated my XUnit packages first, but it did not want to work until I did this as well.
Seems like this bug shows in v15.4.1 of Visual Studio for xUnit v2.2.0 with .NET 4.6.2 on Windows 10 Creators October 2017.
Workaround by @codito works, update to xUnit v2.3.0 also seems to work. Unfortunately update to xUnit v.2.3.0 changes the behavior of the following code and breaks many tests with
Run All
option in Test ExplorerUPDATE#1 xUnit v2.3.0 changes the behavior in the order of the tests. Several tests were written without resetting the default state afterwards. As a result before v2.3.0 I had tests that did not reset the state as last, but now they were executed at the beginning.
UPDATE#2 For those stuck on v2.2.0, I only found @codito workaround to fix this issue. Otherwise, clear
packages
folder, clear%temp%\VisualStudioExtensions
folder and update to xUnit v2.3.0.Seems like in VS 15.4 and after, they added the dotNetCore tests adapter as part of Test Explorer and this conflicts with both test adapters for the same solution.
We have merged the change, though it’s currently untested (I don’t have a machine with .NET 4.7 on it at the moment).
A merged version that can be verified is available as of version
2.3.0-beta4-build3722
, available through MyGet.This problem is not platform dependent . It appears also for other kinds tester nugets. Also for Microsoft’s ; “System.IO.FileNotFoundException: AppData\Local\Temp\VisualStudioTestExplorerExtensions\xunit.runner.visualstudio.2.3.1\build_common\xunit.runner.visualstudio.testadapter.dll” After delete AppData\Local\Temp\VisualStudioTestExplorerExtensions restart the Visual Studio .
2.3.0-beta4-build3722
works for me too.Seeing this on non-SDK csproj too. The workaround works, but to packages folder in the source tree.
Not sure if it’s the same for 2.2.0, but for 2.3.0-beta3-build3705 i also commented out this section of
C:\Users\<user>\AppData\Local\Temp\VisualStudioTestExplorerExtensions\2.3.0-beta3-build3705\build\netcoreapp1.0\xunit.runner.visualstudio.props
to get it to build: