Sharing a unit test withing multiple projects - c#

I have a common db unit test which is used to test naming convetions, it's shared between multiple projects as a an existing file link within visual studio and this runs fine per project ( each project is specific a db).
However it breaks the VS test editor support since the projects with the shared file do not show up in the test view as it shows an error when loading:
Error loading C:\: The test 'DefaultConstraints' from 'c:\listtest.dll' that is loading has the same TestId {3c0c0672-f45b-4b13-697a-77d588b873e4} as the test 'DefaultConstraints' already loaded from 'c:\sandbox\commontest.dll'.
So I can't run the test within VS but can using MSBuild, is there a better way to share common tests?

This is a bug
https://connect.microsoft.com/VisualStudio/feedback/details/574115/unit-tests-error-because-vs-mixes-up-the-debug-and-release-binaries#

Related

Ignoring the load test contained in file 'xyz.loadtest' as it is not supported in Unit Test Explorer

Visual Studio is not able to discover and execute specific unit tests and giving below error while loading the tests :
Ignoring the load test contained in file 'xyz.loadtest' as it is not supported in Unit Test Explorer. To run this test, open the load test file and run it from its editor. For more information see: http://go.microsoft.com/fwlink/?LinkID=236838&clcid=0x409
I went thorough the mentioned reference but didn't find it helpful.
I resolved this issue by updating "MSTest.TestFramework" and "MSTest.TestAdapter" nuget packages to the latest version and setting up "Test->Test Settings->Default Processor Architecture->x64".

OpenCover.Console test fails when code reads data from config file

I have this strange problem. When I use the OpenCover from Visual Studio all tests pass and everything seems fine.
Image from Visual Studio here: http://i.stack.imgur.com/awmWy.png
But when I want to automate the tests using the OpenCover.Console.exe than some tests fail. I have noticed that the 2 failing tests read from config file.
Either the test or the code that is tested reads from config file using ConfigurationManager.AppSettings["someConfigValue"].
I have the same values in the main project and the test project appsettings.
The two failing tests when executing from console image here: http://i.stack.imgur.com/a77Tt.png
In addition I provide the command used for OpenCover.Console.exe.
"OpenCover.Console.exe"
-register
-target:"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\MSTest.exe"
-targetargs:"/noisolation /testcontainer:\"E:\TEAM\TestProject.Tests\bin\Debug\TestProject.Tests.dll" /resultsfile:TestResults.trx"
-mergebyhash
-output:CodeCoverage.xml
UPDATE [04 September 2015]
Since I did not find any particular solution, I came up with a workaround.
Practically I have taken as granted that configuration file and all its data does not belong to unit tests, but to integration tests.
So in my code I do not use ConfigurationManager.AppSettings["someConfigValue"] any more. What I did is created a ConfigurationService and in the IConfigurationService I have all the methods I need for getting the values from the config file.
Having this injected in the BL of the solution, I use Mocks for unit testing purposes.

Sonarqube C# unable to display Integration test/coverage results

We are using TFS/VS 2013 Microsoft build stack to build our .NET ASP application (runs unit tests), then deploys the application based on the build to a web server which then has our API rest and Selenium UI tests ran against it.
I am getting the .coverage and .trx file, and I am able to merge the .coverage into a single .coveragexml. This is able to be processed by sonar and does display the correct results for coverage and test results, but places all the results under 'Unit Test Success or Coverage'.
Is there any way to separate out the results, i.e. Integration Tests or a widget that can display multiple test runs against a single project?
I can somewhat accomplish this by setting up multiple projects (using the same source) and attaching different .coverage and .trx to the project, but this doesn't give a clear picture of the results since the tests are all ran against the same source and bits.
I would like to display our unit tests (not all are .net), C# integration tests, API, and UI tests into separate "widgets"
Here is a copy of my sonar-project.properties file:
# Root project information
sonar.projectKey=XXX.XXX.Presentation
sonar.projectName=XXX.XXX.Presentation
sonar.projectVersion=1.0
# Enable the Visual Studio bootstrapper
sonar.visualstudio.enable=true
sonar.visualstudio.solution=XXX.XXX.sln
sonar.visualstudio.testProjectPattern=.*Tests
# Code Coverage
sonar.cs.vscoveragexml.reportsPaths=MergedResults.coveragexml
# Unit Test Results
sonar.cs.vstest.reportsPaths=TestResults/*.trx
# Some properties that will be inherited by the modules [P.S : Forward slashes]
sonar.sources=.
sonar.projectBaseDir=.
# Info required for SonarQube
sonar.language=cs
sonar.sourceEncoding=UTF-8
Integration code coverage is not yet supported by the C# plugin.
See http://jira.sonarsource.com/browse/SONARNTEST-5
Same story for integration test results:
http://jira.sonarsource.com/browse/SONARNTEST-22

Tests succeed when run from Test View but fail when run from test list editor or command line

I have a test suite, comprised of both unit tests and integration tests, in a project using C# on .NET 4.0 with Visual Studio 2010. The test suite uses MSTest. When I run all tests in solution (either by hitting the button in the testing toolbar or using the Ctrl-R A shortcut chord) all of the tests, integration and unit, pass successfully.
When I either attempt to run the same tests from the command line with mstest (explicitly using the only .testsettings file present) or attempt to run them from the Test List Editor or using the .vsmdi file the integration tests fail.
The integration tests test the UI and so have dependencies on deployment items and such, whereas the unit tests do not. However, I cannot seem to pin down what is actually different between these two methods of running the tests.
When I inspect the appropriate Out directories from the test run, not all of the files are present.
What would cause some of the files that deploy correctly in one situation from Visual Studio to not deploy correctly in another?
The static content started being copied shortly after I wrote the comments above. The other major issue I ran into was that the integration test project referenced libraries that were dependencies of the system-under-test (with copy-local set to true) in order to ensure that the DLLs would be present when they were needed. For some reason, these stubbornly refused to copy when the tests were run through Test List or mstest.
What I eventually did to work around it was include [DeploymentItem] attributes for the DLLs that I needed. This got things working no matter how the tests were run. What I am still unclear on, that may have answered the underlying solution, or provided a better solution, is how Test View/mstest differ from the regular test runner (assuming that the correct .settings file was passed to mstest.). I'm putting these notes/workarounds in an answer, but I'll leave the question open in case anyone can address the underlying cause for how the different test execution paths differ.

Unexpected behaviour from load test

I have a load test which contains only a single unit test.
The unit test is of a function in C# which calls C++ code using C++/CLI wrapper.
It runs well without any exception.
The projects are
1)Business logic ->BusinessLogic.lib
2)Wrapper(takes Business Logic.lib)->Wrapper.dll
3)C# project(takes Wrapper.dll)
The load test on running says that Wrapper.dll is not found whereas the full application runs properly and even unit test doesn't report any such error
The exact error is
Test method TestProject1.testTest.getstateTest threw exception: System.IO.FileNotFoundException: Could not load file or assembly 'ManagedBL.dll' or one of its dependencies. The specified module could not be found
where TestProject1 is the Test Project,testTest is the Test Class and getstateTest is the unit test
Is your load test part of a testing framework (NUnit et al) or is it part of your application?
If the load test is inside a testing framework, then check that the Wrapper.dll gets copied into the "staging" folder where the test framework outputs the DLL and executes the test. Furthermore, please specify exactly what the error message is when you run the load test.
Update
OK, so there are several things that could cause this issue:
Visual Studio does not copy the ManagedBL.dll into the test staging directory.
Visual Studio does not copy the BusinessLogic.lib into the staging directory.
Visual Studio does not copy some other library dependency used by the BusinessLogic.lib (i.e. is the BusinessLogic.lib linking into any other native libraries?)
Technically, VS should copy the ManagedBL.dll if you have added it to the C# project's references; however, do check that it gets properly copied anyway (should be in the TestResults folder).
To fix 2 and 3 you might have to do something like this: How to copy native libraries to the unit test staging directory in Visual Studio 2010
Finally, if all else fails, I would highly recommend that you get the Process Monitor and run it while your test is loading and use the filters to only show the information relative to your test process. Process Monitor should be able to tell you when your process fails to find a file/library.

Categories

Resources