如何让单元测试在x64平台上运行

时间:2023-01-14 12:13:52

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.

我有一个必须在x64模式下运行的c#应用程序。问题是我试图创建在x64应用程序中运行方法的单元测试,而单元测试项目甚至不会构建。当它试图构建时,我得到以下错误。

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.

错误加载C:..... \ bin \ x64 \调试....单元测试。dll:无法加载测试容器的C:..... \ bin \ x64 \调试....单元测试。dll的一个依赖项。如果您将测试项目程序集构建为64位程序集,则无法加载它。在构建测试项目程序集时,为平台选择“任何CPU”。要在64位处理器上使用64位模式运行您的测试,您必须在主机选项卡中更改您的测试设置,以便在32位进程中运行您的测试。错误详细信息:无法加载文件或组装的文件:/ / / C:.....单元测试\ bin \ x64 \调试....单元测试。dll的一个依赖项。尝试加载一个格式不正确的程序。

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?

我已经尝试将单元测试更改为x64以及任何CPU,但仍然无法让它运行。有什么解决办法吗?

5 个解决方案

#1


36  

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.

我假设您正在使用Visual studio测试框架,因为您没有提到其他的。在VS2010中,您可以强制测试主机在x64中运行。将程序集构建为任何CPU或x64,然后将测试主机设置为x64。

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

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

#2


40  

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

在VS 2012及以上版本中,您可以在测试设置|默认处理器体系结构下的测试菜单中选择默认目标平台

如何让单元测试在x64平台上运行

#3


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?

我也有类似的问题。在Visual Studio 2010 (x64下)中有一个奇怪的问题,当您向现有的解决方案添加一个新项目时,这会导致目标CPU被更改。你确定所有的引用和CPU目标都是x64吗?

#4


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)

您需要将测试项目的所有引用设置为local(设置为true)。我之前遇到过这个问题,希望它对你也适用(我使用的是windows 7 64位+ VS2012)

#5


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.

VS需要改进的是,当检测到错误格式异常时,请报告文件名。然后开发人员只能重新构建该文件。

#1


36  

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.

我假设您正在使用Visual studio测试框架,因为您没有提到其他的。在VS2010中,您可以强制测试主机在x64中运行。将程序集构建为任何CPU或x64,然后将测试主机设置为x64。

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

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

#2


40  

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

在VS 2012及以上版本中,您可以在测试设置|默认处理器体系结构下的测试菜单中选择默认目标平台

如何让单元测试在x64平台上运行

#3


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?

我也有类似的问题。在Visual Studio 2010 (x64下)中有一个奇怪的问题,当您向现有的解决方案添加一个新项目时,这会导致目标CPU被更改。你确定所有的引用和CPU目标都是x64吗?

#4


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)

您需要将测试项目的所有引用设置为local(设置为true)。我之前遇到过这个问题,希望它对你也适用(我使用的是windows 7 64位+ VS2012)

#5


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.

VS需要改进的是,当检测到错误格式异常时,请报告文件名。然后开发人员只能重新构建该文件。