VSTest.Console.exe Could not load file or assembly Microsoft.TestPlatform.CoreUtilities
Asked Answered
P

4

8

I'm trying to set up a C# project with the latest version of Visual Studio, 2022; this is with .Net 6 on Windows 10. It's a simple console program, and I've set up the project and a corresponding unit test project basically following the steps described in https://learn.microsoft.com/en-us/visualstudio/test/walkthrough-creating-and-running-unit-tests-for-managed-code?view=vs-2022 so I've got something isomorphic to that tutorial project.

And the unit tests work fine when run from within Visual Studio.

Now I want to also run them from the command line.

vstest.console bin\Debug\net6.0\foo.dll

gives

Testhost process exited with error: Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.TestPlatform.CoreUtilities, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified. File name: 'Microsoft.TestPlatform.CoreUtilities, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' at Microsoft.VisualStudio.TestPlatform.TestHost.Program.Main(String[] args) . Please check the diagnostic logs for more information.

Looks like the toolchain is failing to find one of its own libraries? Is there some option I need to be specifying?

Plio answered 29/3, 2022 at 14:44 Comment(0)
L
17

After upgrading from .Net 5 to .Net 6, I had to face the same issue.

Add the following two lines to your .csproj file to fix the issue.

<CopyLocalLockFileAssemblies>false</CopyLocalLockFileAssemblies>
<GenerateRuntimeConfigDevFile>true</GenerateRuntimeConfigDevFile>

Example: enter image description here

Lyricist answered 12/12, 2022 at 9:1 Comment(0)
T
4

It's a round-about way Visual Studio tells you to use Microsoft.NET.Test.Sdk NuGet in your project with tests.

Thorma answered 25/3 at 22:8 Comment(1)
Thanks, this was indeed the problem I was facing. Just needed to add this nuget to the project.Lentigo
C
2

Make sure that you chose the Correct Assembly which contain the tests and reference MSTest.TestFramework if you follow convention naming, Then Name should be look like FooTests.dll

in the example you mention the following line will throw error

vstest.console ..\BankTests\bin\Debug\net6.0\Bank.dll

While this will run normally

vstest.console ..\BankTests\bin\Debug\net6.0\BankTests.dll
Circumlunar answered 29/3, 2022 at 15:54 Comment(0)
H
2

After upgrading from .NET5 to .NET6 I had the same issue. The solution that worked for me was removing the flag UseCommonOutputDirectory from the test .csproj file.

Then I found this issue that explains why it happened: UseCommonOutputDirectory=true breaks dotnet test #27320

Hexarchy answered 19/7, 2023 at 5:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.