6

I am running .NET Framework 4.6.1 to build a test class library where I build MSTest tests (Microsoft.VisualStudio.TestTools.UnitTesting). I can run the tests absolutely fine by going into the folder containing the test project and running dotnet test.

However, VS does NOT discover the tests in the Text Explorer. I have updated every single related NuGet package to latest as recommended in some answer to this problem:

enter image description here

I have also tried both Test -> Test Settings -> Default Processor Architecture -> x86 and x64.

I always get the following error:

[1/10/2018 5:01:03 PM Warning] Test run will use DLL(s) built for framework Framework45 and platform X86. Following DLL(s) will not be part of run:

What can cause this? If it helps, all my co-workers have the same issue, and only on this solution.

This is a Microsoft.AspNetCore 1.1.2 project.

Edit: csproj:

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

  <PropertyGroup>    
    <TargetFramework>net461</TargetFramework>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <PlatformTarget>x64</PlatformTarget>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <PlatformTarget>x64</PlatformTarget>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="CsvHelper" Version="6.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.0.1" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
    <PackageReference Include="Microsoft.VisualStudio.TestPlatform" Version="14.0.0.1" />
    <PackageReference Include="Moq" Version="4.7.99" />
    <PackageReference Include="MSTest.TestAdapter" Version="1.2.0" />
    <PackageReference Include="MSTest.TestFramework" Version="1.2.0" />
    <PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\CommonHelpers\CommonTestHelpers.csproj" />
    <ProjectReference Include="..\ks-systems\ks-systems.csproj" />
  </ItemGroup>

  <ItemGroup>
    <Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
  </ItemGroup>

</Project>

Edit 2:

Looks like changing to x86 / x64 in the menu bar doesn't change my .csproj. I changed it by hand to x86 - this got them discovered. Now all 56 tests are failing with the following:

Message: Unable to create instance of class cm-tools.Tests.Services.TripScholarshipServiceTest. Error: System.BadImageFormatException: Could not load file or assembly 'CtsPlatform, 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..

Note that making the change above causes the tests to be discovered, and also causes all of them to FAIL, including when trying dotnet test from console, which works without the change.

VSO
  • 11,546
  • 25
  • 99
  • 187
  • 1
    You use AspNetCore *but* you target .NET framework non-core ? (4.6.1) – Pac0 Jan 10 '18 at 22:41
  • @Pac0 Yea - good call, that's how it was set up. I think part of the (not really reasonable) reasoning is that we write NuGet packages for internal stuff, so we need to target that Framework that the packages are written in. I am out of my element here. I am going to go try changing the target framework. Edit: Tried it, cleaned, rebuilt, no change. – VSO Jan 10 '18 at 22:44
  • I suggest [reviewing your `.csproj` files manually](https://stackoverflow.com/a/35991676/). The project could have invalid references and/or conditional logic that is not visible and/or changeable in Visual Studio. – NightOwl888 Jan 10 '18 at 23:11
  • 1
    Also, if you need to share DLLs between .NET Framework 4.5+ and .NET Core, consider targeting [.NET Standard](https://learn.microsoft.com/en-us/dotnet/standard/net-standard). If targeting < .NET Framework 4.5, then create multi-targeted builds/NuGet packages. – NightOwl888 Jan 10 '18 at 23:13
  • 1
    Can you show your test project’s `.csproj` file? – poke Jan 10 '18 at 23:19
  • Can you try removing the `Microsoft.VisualStudio.TestPlatform` package and switching back to x64? Btw. also try to move the `` node into the general unconditioned ``. – poke Jan 12 '18 at 08:27

0 Answers0