I hav开发者_运维问答e a lot of integration tests which read expected results from files. My tests access those files by relative paths. Relative paths are one level of depth different for x86 vs Any CPU. For example, when my tests run under x86, they need to read the following file "../../TestResults/MyTest.csv", but under Any CPU they need to access "../TestResults/MyTest.csv"
So far I have the following constant in every test fixture:
private const string platformDependentPrefix = "";
If I run my tests for x86, I need to manually change "" to "../" in every test fixture.
Is there a way to automate it?
Very hacky way but works:
public static string Platform
{
get
{
if (IntPtr.Size == 8)
return "x64";
else
return "x86";
}
}
Also you can access the CSharpProjectConfigurationProperties3.PlatformTarget
property.
Do you want whether the process is running as 64-bit or what the compilation targets?
If you want the process bitness then you can use the IntPtr.Size method mentioned by Teoman (or Environment.Is64BitProcess if you are using .NET 4).
If you want the targeted platform I would look at Module.GetPEKind in the System.Reflection namespace. The PortableExecutableKinds out parameter will have different values depending on whether you target x86, AnyCPU, or x64 with Required32Bit flag, no flag, PE32Plus flag set, respectively.
You can detect the 'bitness' under which the current process is running using IntPtr.Size. You will either get 4 bytes (32-bit) or 8 bytes (64-bit). There's no such thing as running as Any CPU, but you can have #defines for that configuration that allow you to make decisions at compile-time.
You could add a conditional compilation symbol (Project->Properties->Build) to your project when building in X86 and use this to determine your path.
ex.
#if X86
path = "x86 path";
#endif
In addition to this you may want to create a base test class that all of the tests that use this path inherit from. In this base class is where you would use the compilation symbol. This way you really only have to define your path one time.
精彩评论