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

7

55

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?

Tridentine answered 6/4, 2011 at 19:12 Comment(1)
Does your unit test library have any references that are x86 only?Bohemianism
H
41

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

Hollyhock answered 6/4, 2011 at 19:23 Comment(5)
Yes, sorry, it is Visual Studio 2010Tridentine
great answer and solved my problem. I had to do one more thing to get it to work that is explained here. #2315387Tridentine
Whoops forgot to explain what I had to do. I had to change the setting to Any CPU in the configuration manager as stated here. "You need to ensure the assembly is being built. In my case, this error was due to a combination of the following: (a) in the "Configuration Manager", the "build" button wasn't ticked (b) all other assembles were set to x32, this one was set to "Any CPU" in the "Configuration Manager" (c) it was complaining that it needed an assembly signed so I needed to set this in the project properties"Tridentine
Make sure you don't have x86 reference assemblies when you build for x64.Exsanguine
Is it possible to do the same for the nunit3-console.exe?Chronister
C
68

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

Chough answered 27/7, 2015 at 20:28 Comment(4)
That was the check mark that I missed. I searched for ages all through the Project Properties of both the testee and the testing project, and it came down to that single check mark in the Test Settings... §$%&#*!Metameric
If using Resharper's unit test runner, also check it's Platform: https://mcmap.net/q/267168/-how-to-change-resharper-unit-test-architecture-to-x64Assertive
VS 2017 this was all I needed.Helicon
VS 2019 - that just saved the day for me! Thanks!Munster
H
41

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

Hollyhock answered 6/4, 2011 at 19:23 Comment(5)
Yes, sorry, it is Visual Studio 2010Tridentine
great answer and solved my problem. I had to do one more thing to get it to work that is explained here. #2315387Tridentine
Whoops forgot to explain what I had to do. I had to change the setting to Any CPU in the configuration manager as stated here. "You need to ensure the assembly is being built. In my case, this error was due to a combination of the following: (a) in the "Configuration Manager", the "build" button wasn't ticked (b) all other assembles were set to x32, this one was set to "Any CPU" in the "Configuration Manager" (c) it was complaining that it needed an assembly signed so I needed to set this in the project properties"Tridentine
Make sure you don't have x86 reference assemblies when you build for x64.Exsanguine
Is it possible to do the same for the nunit3-console.exe?Chronister
O
1

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?

Outage answered 6/4, 2011 at 19:17 Comment(0)
I
1

For nunit3 only for users who prefer dotnet test

This work for both .NETcore and .NETFramework projects

dotnet test -- RunConfiguration.TargetPlatform=x64
Itself answered 30/6, 2021 at 23:41 Comment(0)
B
0

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)

Buskined answered 2/3, 2013 at 8:45 Comment(0)
G
0

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.

Ginoginsberg answered 17/7, 2013 at 13:51 Comment(0)
L
0

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

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
</configuration>
Ludwog answered 3/4, 2019 at 12:8 Comment(1)
Explain? your answer, the issue is related to architecture adding appconfig will not help hereRhaetic

© 2022 - 2024 — McMap. All rights reserved.