question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

dotnet test with xUnit and full .NET framework causes System.BadImageFormatException

See original GitHub issue

Steps to reproduce

Create a web application that targets full .NET framework, e.g. with: "frameworks": { "net461": { } },

Create a class library with xUnit, testhost and web application as dependencies - e.g.:

{
  "version": "1.0.0-*",
  "testRunner": "xunit",
  "dependencies": {
    "xunit": "2.1.0",
    "dotnet-test-xunit": "1.0.0-rc2-build10015",
    "Microsoft.AspNetCore.TestHost": "1.0.0-rc2-final",
    "WebApp":  "*" 
  },
  "frameworks": {
    "net461": { }
  }
}

Create a unit test in class library that uses the web application:

public class IntegrationTests
{
    [Fact]
    public void Test()
    {
        var builder = new WebHostBuilder();
        builder.UseStartup<Startup>();
    }       
}

Expected behavior

Tests are executed properly

Actual behavior

Project WebApp (.NETFramework,Version=v4.5.1) was previously compiled. Skipping compilation.
Project WebApp.Tests (.NETFramework,Version=v4.6.1) will be compiled because inputs were modified
Compiling WebApp.Tests for .NETFramework,Version=v4.6.1

Compilation succeeded.
    0 Warning(s)
    0 Error(s)

Time elapsed 00:00:01.4138420


xUnit.net .NET CLI test runner (32-bit win81-x86)
  Discovering: WebApp.Tests
  Discovered:  WebApp.Tests
  Starting:    WebApp.Tests
      System.BadImageFormatException : Could not load file or assembly 'WebApp, 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.
      Stack Trace:
           at WebApp.Tests.IntegrationTests.Test()
  Finished:    WebApp.Tests
=== TEST EXECUTION SUMMARY ===
   WebApp.Tests  Total: 1, Errors: 0, Failed: 1, Skipped: 0, Time: 0,193s

Environment data

dotnet --info output:

.NET Command Line Tools (1.0.0-preview1-002702)

Product Information:
 Version:     1.0.0-preview1-002702
 Commit Sha:  6cde21225e

Runtime Environment:
 OS Name:     Windows
 OS Version:  6.3.9600
 OS Platform: Windows
 RID:         win81-x64

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:30 (1 by maintainers)

github_iconTop GitHub Comments

51reactions
RehanSaeedcommented, Jul 4, 2016

Visual Studio Fix

Menu Bar -> Test -> Test Settings -> Default Processor Architecture -> X64

8reactions
felschrcommented, Mar 8, 2017

After upgrading my .NET Core project which targets net461 to csproj I have this issue again when trying to run dotnet test in the command line it always runs the x86 build.

Read more comments on GitHub >

github_iconTop Results From Across the Web

dotnet test with xUnit and full .NET framework causes ...
I can confirm that after moving a project to VS17 and csproj I constantly get System.BadImageFormatException when running from the command line.
Read more >
System.BadImageFormatException when running XUnit ...
System.BadImageFormatException when running XUnit 2.2.0 unit test in VS 2017 against a full .net framework asp.net core project.
Read more >
How to fix System.BadImageFormatException
BadImageFormatException is usually due to bitness. One project is 64 bit and the the other is 32 bit. Make sure both project target...
Read more >
Can't debug xunit tests under windows or mac : RIDER-49039
I can't debug anymore any xunit test with Jetbrains Rider ever in Mac or Windows. ... System.BadImageFormatException: Could not load file or assembly ......
Read more >
Test runner won't execute .NET core tests in 32 bit
Hello I have the following scenario. Test Subject: A dotnet standard class library which accesses a managed dll. This dll needs to be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found