Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VSTest fails to run tests in .NET 6 test project #2985

Closed
adammodlin opened this issue Jul 16, 2021 · 10 comments
Closed

VSTest fails to run tests in .NET 6 test project #2985

adammodlin opened this issue Jul 16, 2021 · 10 comments

Comments

@adammodlin
Copy link

adammodlin commented Jul 16, 2021

Steps to reproduce

I created a .NET 6 MAUI Blazor application (dotnet new maui-blazor). I added a Test Project and a very basic test (just write to console). When I try to run tests, I get the error shared below. This happens in both Visual Studio 2019 Preview and command line.

Expected behavior

Expect the test to run.

Actual behavior

No tests are run and the following is output in the Tests output

Starting test discovery for requested test run
========== Starting test discovery ==========
Microsoft.VisualStudio.TestPlatform.ObjectModel.TestPlatformException: Testhost process exited with error: A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\Program Files\dotnet'.
Failed to run as a self-contained app.
  - The application was run as a self-contained app because 'C:\code\Microsoft-DevOps-Command-Center\src\DOCC.Azure.UnitTests\bin\Debug\net6.0\testhost.runtimeconfig.json' was not found.
  - If this should be a framework-dependent app, add the 'C:\code\Microsoft-DevOps-Command-Center\src\DOCC.Azure.UnitTests\bin\Debug\net6.0\testhost.runtimeconfig.json' file and specify the appropriate framework.
. Please check the diagnostic logs for more information.
   at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyOperationManager.ThrowOnTestHostExited(Boolean testHostExited)
   at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyOperationManager.SetupChannel(IEnumerable`1 sources, String runSettings)
   at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyDiscoveryManager.DiscoverTests(DiscoveryCriteria discoveryCriteria, ITestDiscoveryEventsHandler2 eventHandler)
========== Test discovery aborted: 0 Tests found in 94.5 ms ==========
Executing test method: DOCC.Azure.UnitTests.AzureClientTests.AzureClient_Ctor_Success
========== Starting test run ==========
========== Test run finished: 0 Tests (0 Passed, 0 Failed, 0 Skipped) run in < 1 ms ==========

Diagnostic logs

log.txt

Environment

Running on Windows 10 19043.1110

@adammodlin
Copy link
Author

#2218 appears to be the same behavior that was supposedly fixed.

@nohwnd
Copy link
Member

nohwnd commented Jul 19, 2021

Is there Microsoft.NET.Test.SDK installed in your project? What is the version?

@adammodlin
Copy link
Author

Yes, it is. It's version 16.10.0.
image

@nohwnd
Copy link
Member

nohwnd commented Jul 22, 2021

Does this happen with a specific project or any project you generate from dotnet new? Are you able to share you project or at least the project file?

What is the output of dotnet --info when run in the project folder? Are you using global.json?

That error can have multiple causes, and it is never easy to know exactly which one it is, unfortunately.

Here I have a project that should IMHO repro the issue that was fixed. It works on my machine:

<!-- file mstest001.csproj -->
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <OutputType>Exe</OutputType>
    <RuntimeIdentifier>win10-x64</RuntimeIdentifier>
    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
    <PackageReference Include="MSTest.TestAdapter" Version="2.2.5" />
    <PackageReference Include="MSTest.TestFramework" Version="2.2.5" />
    <PackageReference Include="coverlet.collector" Version="3.0.3" />
  </ItemGroup>

</Project>
// file UnitTest1.cs
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace mstest001
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
        }
    }
}
C:\t\mstest001> dotnet --version
6.0.100-preview.6.21355.2

C:\t\mstest001> dotnet test
  Determining projects to restore...
  All projects are up-to-date for restore.
  You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
  mstest001 -> C:\t\mstest001\bin\Debug\net6.0\win10-x64\mstest001.dll
Test run for C:\t\mstest001\bin\Debug\net6.0\win10-x64\mstest001.dll (.NETCoreApp,Version=v6.0)
Microsoft (R) Test Execution Command Line Tool Version 17.0.0-preview-20210518-01
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.

Passed!  - Failed:     0, Passed:     1, Skipped:     0, Total:     1, Duration: 13 ms - mstest001.dll (net6.0)

@adammodlin
Copy link
Author

I'm not using a global.json.

PS C:\code\Microsoft-DevOps-Command-Center\src\DOCC.Azure.UnitTests> dotnet --version
6.0.100-preview.6.21355.2
PS C:\code\Microsoft-DevOps-Command-Center\src\DOCC.Azure.UnitTests> dotnet test
  Determining projects to restore...
  All projects are up-to-date for restore.
  You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
  DOCC.Azure -> C:\code\Microsoft-DevOps-Command-Center\src\DOCC.Azure\bin\Debug\net6.0\DOCC.Azure.dll
  DOCC.Azure.UnitTests -> C:\code\Microsoft-DevOps-Command-Center\src\DOCC.Azure.UnitTests\bin\Debug\net6.0\DOCC.Azure.UnitTests.dll
Test run for C:\code\Microsoft-DevOps-Command-Center\src\DOCC.Azure.UnitTests\bin\Debug\net6.0\DOCC.Azure.UnitTests.dll (.NETCoreApp,Version=v6.0)
Microsoft (R) Test Execution Command Line Tool Version 17.0.0-preview-20210518-01
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
Testhost process exited with error: A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\Program Files\dotnet'.
Failed to run as a self-contained app.
  - The application was run as a self-contained app because 'C:\code\Microsoft-DevOps-Command-Center\src\DOCC.Azure.UnitTests\bin\Debug\net6.0\testhost.runtimeconfig.json' was not found.
  - If this should be a framework-dependent app, add the 'C:\code\Microsoft-DevOps-Command-Center\src\DOCC.Azure.UnitTests\bin\Debug\net6.0\testhost.runtimeconfig.json' file and specify the appropriate framework.
. Please check the diagnostic logs for more information.

Test Run Aborted.
PS C:\code\Microsoft-DevOps-Command-Center\src\DOCC.Azure.UnitTests>

Here is my project.json:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>

    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Identity.Client" Version="4.34.0" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
    <PackageReference Include="MSTest.TestAdapter" Version="2.2.5" />
    <PackageReference Include="MSTest.TestFramework" Version="2.2.5" />
    <PackageReference Include="coverlet.collector" Version="3.0.3">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\DOCC.Azure\DOCC.Azure.csproj" />
  </ItemGroup>

</Project>

I also have an empty TestClass like yours above. This is basically in a totally fresh skeleton .NET project created with command:

dotnet new maui

@nohwnd
Copy link
Member

nohwnd commented Jul 23, 2021

Run $env:COREHOST_TRACE=1; $env:COREHOST_TRACE_VERBOSITY=4; dotnet test from powershell to see the most verbose logs of dotnet host resolve. There will be multiple, resolves. The last resolve will be for the testhost. It is thousands line long, but if you find Starting test execution, please wait.., the info of how a match was found, and if global.json was considered should be in the first 100 or so lines.

@jfversluis
Copy link
Member

jfversluis commented Aug 12, 2021

While searching to make my unit tests work for https://github.com/CommunityToolkit/Maui/tree/main/src/CommunityToolkit.Maui.UnitTests I came across this. I seem to run into the same thing and it is reproducible with this locally, on a Mac that is. Didn't try Windows yet. Exact same happens on Windows.

It seems weird that we're building the android TFM though, but anyway, while building it as is the error is:

Unable to find /Users/jfversluis/Source/Maui-toolkit/src/CommunityToolkit.Maui.UnitTests/bin/Debug/net6.0-android/CommunityToolkit.Maui.UnitTests.deps.json. Make sure test project has a nuget reference of package "Microsoft.NET.Test.Sdk".

If I set the TFM to be net6.0, which seems to make more sense, the error changes to

Testhost process exited with error: A fatal error was encountered. The library 'libhostpolicy.dylib' required to execute the application was not found in '/Users/jfversluis/Source/Maui-toolkit/src/CommunityToolkit.Maui.UnitTests/bin/Debug/net6.0/'.
Failed to run as a self-contained app.

Let me know if there is anything you want me to try :)

@brminnick
Copy link

brminnick commented Oct 18, 2021

For .NET MAUI apps, this is now fixed and will be included in the next .NET MAUI preview: dotnet/maui#3017

@nohwnd nohwnd closed this as completed Oct 19, 2021
@Testadora
Copy link

I am having this same issue for a .NET 6 C# test automation solution, using MS .NET Test sdk 17.2.0, VS-2022, Windows-2022 on Microsoft hosted agent. https://developercommunity.visualstudio.com/t/errors-when-running-tests-with-VsTest-in/10066577
Wondering if anyone has successfully gotten VsTest to work with the newest upgrades?

@aghasemiN
Copy link

I am having this same issue for a .NET 6 C# test automation solution, using MS .NET Test sdk 17.2.0, VS-2022, Windows-2022 on Microsoft hosted agent. https://developercommunity.visualstudio.com/t/errors-when-running-tests-with-VsTest-in/10066577 Wondering if anyone has successfully gotten VsTest to work with the newest upgrades?

I have the same issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants