How do I get unit tests to run on a x64 platform

C#.Netasp.netVisual Studio

C# Problem Overview


I have a C# application that must run in x64 mode. The problem is I try to create unit tests that run methods in the x64 application and the unit test project won't even build. I get the following error when it tries to build.

> Error loading C:.....\bin\x64\Debug....UnitTests.dll: Unable to load > the test container 'C:.....\bin\x64\Debug....UnitTests.dll' or one > of its dependencies. If you build your test project assembly as a 64 > bit assembly, it cannot be loaded. When you build your test project > assembly, select "Any CPU" for the platform. To run your tests in 64 > bit mode on a 64 bit processor, you must change your test settings in > the Hosts tab to run your tests in a 32 bit process. Error details: > Could not load file or assembly > 'file:///C:.....UnitTests\bin\x64\Debug....UnitTests.dll' or one of > its dependencies. An attempt was made to load a program with an > incorrect format.

I have tried to change the unit test to x64 and also Any CPU and still cannot get it to run. Is there any fix for this?

C# Solutions


Solution 1 - C#

In VS 2012 and above you can select the default target platform from Test menu under Test Settings|Default Processor Architecture

Visual Studio Test Settings

Solution 2 - C#

I'm assuming that you are using Visual Studios testing framework since you mention no other. In VS2010 you can force the testing host to run in x64. Build your assemblies as Any CPU or x64 then set the test host to x64.

http://msdn.microsoft.com/en-us/library/ee782531.aspx

Solution 3 - C#

I had a similar issue. There is a strange issue in Visual Studio 2010 (under x64), which causes the target CPU to be changed when you add a new project to the existing solution. Are you sure that all references and CPU targets are x64?

Solution 4 - C#

I have had the same issue, Add App.config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
</configuration>

Solution 5 - C#

You will need to set all references of the test project to local (set to true). I got this problem before, hope that it will work for you too (I'm using windows 7 64 bit + VS2012)

Solution 6 - C#

This error can be fixed if you clean and rebuild all of the relevant assemblies. There must be one file corrupted unexpectedly.

One thing for VS to improve, is when the bad format exception is detected, please report the file name. Then the developer can only rebuild that file.

Solution 7 - C#

For nunit3 only for users who prefer dotnet test

This work for both .NETcore and .NETFramework projects

dotnet test -- RunConfiguration.TargetPlatform=x64

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRJ.View Question on Stackoverflow
Solution 1 - C#Sten PetrovView Answer on Stackoverflow
Solution 2 - C#Robert JeppesenView Answer on Stackoverflow
Solution 3 - C#Svetlin RalchevView Answer on Stackoverflow
Solution 4 - C#Sargis GhazaryanView Answer on Stackoverflow
Solution 5 - C#thienhaflashView Answer on Stackoverflow
Solution 6 - C#CaryView Answer on Stackoverflow
Solution 7 - C#Chui TeyView Answer on Stackoverflow