microsoft / testfx

MSTest framework and adapter
MIT License
749 stars 255 forks source link

Error in Unit test targetting net462 in VS2017 #177

Closed skynetit closed 7 years ago

skynetit commented 7 years ago

Description

I have a ASP.NET core application targeting net462 as I need to use EventLogTraceListener. I created a .net core Unit test project and targeted the project to net462. When I tried to run the test from VS2017, I got the error.

An exception occurred while invoking executor 'executor://mstestadapter/v2': Constructor on type 'Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.UnitTestRunner' not found.

Steps to reproduce

  1. Create a ASP.NET core project and target net462.
  2. Create a .NET core MSTest unit test project and target net462 as the ASP.NET core targets net462.
  3. Run testf rom VS2017.

Expected behavior

Test should run

Actual behavior

Failed with error An exception occurred while invoking executor 'executor://mstestadapter/v2': Constructor on type 'Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.UnitTestRunner' not found.

Environment

MSTest version 1.1.17

How to get codecoverage when I want to run using vstest.console.exe?

AbhitejJohn commented 7 years ago

@mailbala : Is this an upgrade from an older version of the package? Is yes, can you try after restarting VS please? This is a known caching issue in VS we have been hitting for a while now.

On the code coverage part, you could run vstest.console with a /enablecodecoverage argument.

skynetit commented 7 years ago

@AbhitejJohn Initially I tried upgrading from older version. I got the same error. So I created as a new application but same error. I tried restarting VS as well

AbhitejJohn commented 7 years ago

@mailbala : Can you also clear the following cache folders as well:

  1. %temp%\VisualStudioTestExplorerExtensions.
  2. <ProjectPath>\packages
  3. C:\users\<UserName>\.nuget\packages\mstest.testadapter
  4. project output folder(bin\debug)

Also are both of these projects targeting the same version(1.1.17) of the mstest framework and adapter?

waratah commented 7 years ago

Had same error. Just upgraded to 15.2 still same error. Cleaned the above, No change.

Opened original solution that I copied to new directory and modified and it runs OK.

Seems to be something amiss with the new solution not with the installation.

AbhitejJohn commented 7 years ago

@waratah : which solution are we referring to? @mailbala : Checking back to see if the above worked for you.

skynetit commented 7 years ago

@AbhitejJohn I followed the steps mentioned above but still facing the same issue

AbhitejJohn commented 7 years ago

@mailbala : Can you set a system level environment variable, VS_UTE_Diagnostics to 1 to get diagnostic logs in the Output -> Tests pane of VS and share that here please?

waratah commented 7 years ago

@abhitejjohn. I tried all the refresh steps. None worked. Including updating ms test packages.

I upgraded visual studio 2017.2 and this time it worked ok.

skynetit commented 7 years ago

@AbhitejJohn After upgrading to VS2017 version 15.2. I am not facing the above issue.

The WebAPi project targets net462 and Output type is exe . In the unit test project, I am using NSubstitute to mock the dependencies. When I run the test, I am getting the below error

Message: Initialization method WebApi.UnitTest.Controllers.HomeControllerV1Test.TestInitializer threw exception. System.IO.FileLoadException: Could not load file or assembly 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040).

When I use Moq, getting the below error.

Message: Test method WebApi.UnitTest.Controllers.HomeControllerV1Test.TestUserList threw exception: System.BadImageFormatException: Could not load file or assembly 'CustomerInsights.WebApi, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.

AbhitejJohn commented 7 years ago

@mailbala : Thanks for the update! I created another issue for the above. Will use this for tracking the IDE caching issue.

TonyValenti commented 7 years ago

I'm getting this error too. What is the workaround?

nickalbrecht commented 7 years ago

I ran into this issue today too. I had added a new Test project to an existing solution, immediately updated the Nuget packages for MSTest to the most recent verion, and was getting this error. Restarting VS2017 (15.2) resulted in the error going away.

AbhitejJohn commented 7 years ago

@TonyValenti : Do let us know if this is still blocking. As @nickalbrecht confirmed a restart of VS should fix this.

PeterWaher commented 7 years ago

I have the same problems. Clearing files as instructed above, restarting VS 2017, error persists. My project is very simple: A .NET standard 1.3 class library, consumed by a Unit test class library with one test, using .NET FW 4.6. All compiles well. I get the following in my output window, when trying to run the test:

------ Run test started ------ An exception occurred while invoking executor 'executor://mstestadapter/v2': Konstruktorn för typen Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.UnitTestRunner hittades inte. ========== Run test finished: 0 run (0:00:00,7004946) ==========

(Swedish exception, translated into "Constructor for type Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.UnitTestRunner not found.")

GitHub repository: https://github.com/PeterWaher/IoTGateway Unit test project reporting error: https://github.com/PeterWaher/IoTGateway/tree/master/Networking/Waher.Networking.MQTT.Test .NET standard 1.3 library being tested: https://github.com/PeterWaher/IoTGateway/tree/master/Networking/Waher.Networking.MQTT

AbhitejJohn commented 7 years ago

@PeterWaher : It looks like the repo has multiple versions of the framework/adapter here. That causes a problem. Unfortunately, today a single version of the adapter is used for a solution and the version picked up is not determinate. Moving all of these projects to a single versions (1.1.18) should get you going. I've filed https://github.com/Microsoft/testfx/issues/194 to better track this.

PeterWaher commented 7 years ago

@AbhitejJohn : Thanks. This solved the issue. After having updated all adapter versions to the same version, the error disappeared.

eakoning commented 7 years ago

In my case consolidating multiple versions of the MSTest.TestAdapter and MSTest.TestFramework NuGet Packages to the latest version solved it.

pvlakshm commented 7 years ago

Please see here: https://github.com/Microsoft/vstest-docs/blob/master/RFCs/0013-Test-Adapter-Lookup.md for guidance. The upgrade issue has been fixed as of VS 15.5 Preview 1.