How to run unit tests for x86 project - unit-testing

I'm trying to add unit tests for my x86 .NET Core project.
I've created new project inside my solution and Visual Studio made it x64 by default. I added reference to the new project and tried to run an example test but it failed because there was a mismatch between projects' platform targets - x86 vs x64.
Then I tried to change platform target to x86 in the project with tests.
It gives me these warnings:
Test run will use DLL(s) built for framework .NETCoreApp,Version=v1.0 and platform X86. Following DLL(s) do not match framework/platform settings.
Project.UnitTests.dll is built for Framework 2.1 and Platform X86.
Exception discovering tests from Project.UnitTests: System.BadImageFormatException: Could not load file or assembly 'Project.UnitTests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. An attempt was made to load a program with an incorrect format.
No test matches the given testcase filter `FullyQualifiedName=Project.UnitTests.UnitTest1.Test1
Any ideas how to make it work?
Thanks

Related

Visual Studio Unit Test 32-bit and 64-bit

I have a solution that has projects both with C++ and C++/CLI code, and a set of projects which unit test all of these, using the Microsoft Unit Test Framework. For the C++/CLI projects, the unit test projects are C# unit tests. What I currently have is a platform for 32- and 64-bit. Also, for each platform I have unit test projects set to 32- and 64-bit platforms to match.
The issue I have is that when I switch to 32-bit vs. 64-bit I need to go to
TEST > TEST SETTINGS > DEFAULT PROCESSOR ARCHITECTURE and flip from 32- and 64 as needed. If I don't, I get a warning from Visual Studio that a 64-bit image cannot run in a 32-bit process. This makes sense, but surely there is some way to automate this?
Otherwise if I do a batch build on a build machine I will not have control of this and the unit tests will fail.
Also I have tried to set the unit test projects to be AnyCPU but this fails with an error saying "An attempt was made to load a program with an incorrect format"
Is there a better way perhaps?
If you're looking to automate test run i a build machine you can set the project to AnyCPU and run corflags /32bit+ (or /32bit-) to set the .NET assembly to the right platform before running the unit tests.
I'm not aware of an automatic solution for VS other then have two projects (one x64 and on x86) that link to the same files.

Running mstest from command line against Visual Studio 2012 native C++ tests

I have a Visual Studio 2012 Solution with a number of native c++ test projects.
I can run all of these correctly and successfully from within Visual Studio 2012 using the Test Explorer tab.
However, I cannot get the tests to run when running from the command line.
Following the documentation I have been running the following command line
mstest /testcontainer:PathToTestProject\Win32\Release\testproject.dll
I also need to run
mstest /testcontainer:PathToTestProject\x64\Release\testproject.dll
for the testing of the 64bit version of the code.
When I run these command lines I get the following error message.
Microsoft (R) Test Execution Command Line Tool Version 11.0.50727.1
Copyright (c) Microsoft Corporation. All rights reserved.
Loading PathToTestProject\Win32\Release\testproject.dll...
PathToTestProject\Win32\Release\testproject.dll
Unable to load the test container PathToTestProject\Win32\Release\testproject.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:\PathToTestProject\Win32\Release\testproject.dll' or one of its dependencies. The module was expected to contain an assembly manifest.
The code is native c++ and has two build configurations one on Win32 platform, and the other on x64 platform. I cannot have an AnyCPU platform configuration.
What am I missing here to be able to run the tests from the command line?
After a lot of searching, I finally discovered a very hidden msdn documentation page
here which states the compatibility of mstest with different test project types.
And it turns out the mstest is not compatible with native unit tests (nice of msdn to document this in an easy to find location).
Instead you need to use the visual studio test running (vstest.console.exe) instead of msbuild for native unit test projects.
for example
vstest.console.exe /Platform:x64 PathToTestProject\x64\Release\testproject.dll

How can I disable code coverage / assembly instrumentation in Visual Studio 2012?

I have a project upgraded from Visual Studio 2010 to 2012 and the .testrunconfig file was included in the upgrade process.
I noticed that it was possible to click "Analyze code coverage" on any of the unit tests that I had run and it would correctly display the result. However, my test run configuration (originally from VS 2010) had code coverage disabled.
After doing a bit of research I learned that the VS 2010 configuration files have been deprecated and replaced by .runsettings files. It would appear that VS 2012 enforces assembly instrumentation by default which has a massive overhead associated with it.
Therefore, I would like to know how I can disable code coverage in VS 2012. Based upon my current findings it does not seem to be a trival task. One recent article I read had me creating an XML file manually and naming it "MYCONFIGURATION.runsettings" and manually manipulating XML attribute values.
Does anyone know how this should be done?
This is what I understand from your post:
You have a Test project with .testsettings file. You have not enabled code coverage in the test settings.
Code coverage instrumentation is not enabled by default in your scenario. Binaries will be instrumented if you do 'analyze code coverage' from VS.
Additional Info:
You can confirm that .coverage file is not generated by running the following command from visual studio developer command prompt:
vstest.console.exe /Settings:<your test settings file> test.dll
A coverage file will only get generated if you have enabled coverage in test settings.
Code coverage is only enabled through the Test Explorer using data driven adapters. The metadata for tests ran through the test explorer is almost completely different than that of tests ran straight from the unit test session window. Have you tried simply running it straight from the code (the MSTest gui bubbles) or from the unit test session window?

Where to put 'useLegacyV2RuntimeActivationPolicy' for build in TFS2012

While i'm locally get the error in a unit test:
Mixed mode assembly is built against version 'v2.0.50727' of
the runtime and cannot be loaded in the 4.0 runtime without
additional configuration information
And the solution is to add
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
</configuration>
to the config file located in
c:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE
\CommonExtensions\Microsoft\TestWindow\vstest.executionengine.x86.exe.config
This still is going wrong on our TFS2012 build server. I also added the startup tags to the file on the TFS2012, but the build server reports the error.
How do I get this to work on a unit test project (using the new unit test framework of VS2012 & TFS2012) in a build definition?
While having problems to get it running on TFS too, you might give it a try to set it programmatically (e.g. using this approach). While I would not recommend using that in productive code it should be perfectly sufficient when it comes to UnitTests.

TFS 2010 Build Automation - Enabling Code Coverage

How do you enable code coverage for unit testing in TFS2010 automatic build?
In Visual Studio 2010 we enabled Code Coverage for our test projects (Test --> Edit Test Settings --> Local Test Settings --> enable Code Coverage and choose assemblies to run code coverage against).
Now this is working fine (we can read code coverage) when running from Visual Studio 2010 (Test --> Run --> All Test In Solution).
The problem is that no code coverage is reported in TFS 2010 when building.
Note that the test projects are used by the build controller but without any code coverage.
Is very important for us to enable code coverage together with unit testing.
I forgot to mention that the problem I'm encountering has to do with code coverage for a web application project.
At step:
Test --> Edit Test Settings --> Local Test Settings --> enable Code Coverage and choose assemblies to run code coverage against
when choosing assemblies you can choose the web application. I think that the problem is related to Path (the value under the Path column from wizard) which is the one from developer machine (http://localhost...). When building and deploying with TFS the build goes on one server and the deployment on another.
The question should be now: which path should be available in testsettings. The build path or the deployment path?
Currently I'm not at work and can't test my doubts.
I'll let you know as soon as I get in touch with TFS.
You need to tell the build server which test settings file to use. You will find this in the build settings under Process >2. Basic >Automated Tests >1. Test Assembly >TestSettings file.
Once you have that specified then the Code coverage should work.