Visual Studio 2017: Run tests with Startup Projects - c#

I have a solution containing a few projects, three of which startup when hitting the Start button, which are integral to the operation of the program. I have some tests, which have been found by Visual Studio and are inside the Test Explorer panel.
The question is: When I hit the Run All button inside the Test Explorer panel, how can I tell Visual Studio to startup the same projects as when I hit the Start button?
The only way I can see it being done is by using a *.testsettingsfile to use a Setup script that will run the projects, but it just feels like a dirty way of doing it.
Any ideas?

Open two visual studio apps, one for running your three projects and the other for running the tests. But I think only functional/integration tests need to test the executions which are running currently.

The way I have accomplished this in the past for system tests that need the main executable running is to leverage the Assembly Initialize in the test project and have it start the required exe.
We have a build setup that puts all of our debug output into a common location, so we can find the appropriate exe because it is in the same path as the test assembly.
[AssemblyInitialize]
public static void AssemblyInitialize(TestContext context)
{
// Launch the exe here.
}

Related

Profile unit test in Visual Studio 2017 + NUnit + Resharper

I have a C# project in Visual Studio 2017 Professional. It has some unit tests: methods declared with [Test] which display a little green tick icon in the editor, where I can right-click and 'Run' or 'Debug' the test. When I do so, a window opens with all tests in the solution and I can click a green 'play' triangle to run them all.
I can also run my program under the profiler starting from Debug -> Performance Profiler.
So far so good. Now, how do I run a unit test under the profiler?
When I first asked this question I looked for profiling unit tests and saw it was a feature in Visual Studio Professional. But in fact, I'm not using VS's own unit testing support, but rather NUnit, driven by Resharper. Sorry to throw everyone off the scent.
Using VS2017 Professional and the Diagnostic Tools you can profile code between two break points!
Thus, you can debug your unit test (xunit, nunit, mstest, whatever!) with any tool (VS Test Explorer, ReSharper, etc) like this:
enable diagnostic tools while debugging (see answer from tm1)
Set a break point at the start of your unit test, another one at the end (or wherever you like)
Debug the unit test with your favorite tool, you hit the first break point.
Open the Diagnostic Tools window (Debug > Windows > Show Diagnostic Tools)
Start Recording a CPU profile (small record button below all those graphs)
Continue (F5) to the next break point
VS will now create a snapshot for this period and you can dig into the relevant methods. Voila!
Just for completeness, an alternative, commercial solution is Jetbrain's DotTrace, which is part of ReSharper Ultimate at the point of writing. Here you could directly profile your unit test without manual break points.
If you can debug and stop at a break-point, you should also be able to get CPU and memory usage according to this page, given you enable diagnostics tools while debugging.
Edit
According to #patrick-stalph the 3-step proposed solution does not work in Visual Studio 2017 Professional.
Disclaimer
I'm using Visual Studio Enterprise 2017, so I cannot check, whether this works in Professional, or not.
Open Test Explorer e.g. Ctrl + Q, then type Test Explorer and hit Enter
You should see your tests in Test Explorer. If you cannot, Build.
Choose a test, right click it, and choose Profile Test
Workaround
I must note, that I'm using xunit and autofixture, and I haven't seen this feature working. See this issue, why I haven't.
Hence, based on the answer of Jeremy Liberman, I was able to work around using the steps below:
Install xunit.runner.console NuGet package to the test project
Click Analyze > Performance Profiler... from the Toolbar.
Change Target to Executable
Click Start
Follow the Wizard, till you reach Choose the application that you want to profile (.EXE, .DLL, Website)
Choose An executable (.EXE file)
Based on Run tests with the xUnit.net console runner, I've specified the following values:
Full path: c:\<...>\src\packages\xunit.runner.console.2.4.0\tools\net472\xunit.console.exe (fit this to your environment)
Command-line arguments: <Product>.Tests\bin\Debug\<Product>.Tests.dll
Working directory: c:\<...>\src\ (the root of the Solution)
Next, Finish
Note, that this profiles all of the tests in the test assembly. You can choose, which tests you want to run by providing more Command-line arguments to xunit.runner.console. To learn more about the console runner options, run the console runner with no command line options.

Visual Studio c# should NOT start IIS website when debugging unit tests

I have a VS (2013) solution containing two unittest project's and a seperate webproject. The webproject has no relation to the unittest projects. The unittest projects is covering integration test of another solution and the webproject is a dashboard showing the results of these tests. They dont require each other to run as the test results are stored on an external server.
My issue is:
When I debug one of my tests (right click + debug test), the webproject is also started and the IIS express will be running. While this does not affect my tests, it is very annoying. How do I make sure the webproject is not automatically started when debugging the tests? The project is NOT set as startup project.
I know that I could simply create two separate solutions, but I want to keep the projects in a single solution.
Thank you for your help.
Click on your webproject, hit F4, there is an option "Always start when debugging", you should turn-off this option.

Visual Studio 2013 doesn't discover unit tests

I have a simple solution in visual studio 2013 that is composed by one web project, one library project and one unit test project. When I open the solution and try to run the unit tests they are not discover by visual studio. To run the tests I try to go to the menu and choose Test -> Run -> Run all tests or by opening the test explorer window. By those to methods visual studio doesn’t discover any tests in the solution.
Creating first a simple unit tests project and try to run the test, visual studio know discover the test and I can run it. Then, if I open my previous solution visual studio now discovers all the tests. I try to save my solution but closing it and reopening, without creating a unit test project first, the visual studio doesn’t find the tests again. This is a very strange behave that I don’t know why this is happening.
I used to working alone in this project that was using the source control git integrated with the visual studio team foundation. The problem of visual studio not discover the unit tests start when a new element came to the project and when I need to recreate the solution through the source control online. Before this, all tests always been discovered by visual studio.
For creation the unit tests I use the dll Microsoft.VisualStudio.QualityTools.UnitTestFramework. My version of visual studio is: Microsoft Visual Studio Express 2013 for Web Version 12.0.30723.00 Update 3. My version of .net framework is 4.5.50938.
All of my tests are like this:
[TestClass]
public class Service1Test
{
[TestMethod]
public void Test1()
{
Assert.IsTrue(True);
}
}
Some things I've noticed I have to do from time to time to get tests to show up properly.
If your solution is in a protected drive that you need administrator access to read/write, sometimes only a portion of the tests come up. Definitely run VS as administrator in that case.
If your solution is 64 bit, make sure that Test > Test Settings > Default Processor Architecture is set to x64. Sometimes it gets set to x86. Set it to x64, then rebuild.
Sometimes just restarting Visual Studio does the trick because the test explorer will start up again.
Don't forget to actually build the test project/solution. (If you want it to get built with the rest of the projects, right-click on your solution > Properties > Configuration Properties > Configuration > check the "Build" box for your test project)
Ensure the tests are in a public section of your test class
If you using NUnit, make sure to download NUnit Adapter first.
Go to Tools → Extensions and Updates… → Online → search for "NUnit Test Adapter".
Make sure your test class is public so it can be found. And if you're referencing another class, make sure of the same.
Also, sometimes if you have no Asserts or you're not decorating the test with a [TestMethod], a test might not be recognized.
2 more things:
1) Async unit tests act funny at best, and none at all at worst. Have a look at this article by Stephen Cleary and keep from there if it interests you.
2) If you use NUnit and you run into the same issues, keep in mind it's [TestCase] for Nunit, instead of [TestMethod]
Having said the above, here's an article I've posted on the code project, with both MSTest & NUnit, in case you want to give it a spin and make sure you're not missing anything.
I had the same issue but none of the other solutions worked. Turns out that I was using the NUnit 3 framework with the 2 adapter.
If you're using NUnit 3, go to Extensions and Updates and install the NUnit3 Test Adapter.
I'm having this issue from time to time. What works for me is to shutdown Visual Studio and go to folder:
%LocalAppData%\Microsoft\VisualStudio\12.0\ComponentModelCache
and delete it content.
Once you open Visual Studio and load your project again Test Explorer should contain back your tests
XUnit users may notice Test Explorer window no longer lists any tests. To make tests discoverable again try this important tip, highlighted below.
If you're having problems discovering or running tests, you may be a victim of a corrupted runner cache inside Visual Studio. To clear this cache, shut down all instances of Visual Studio, then delete the folder %TEMP%\VisualStudioTestExplorerExtensions. Also make sure your project is only linked against a single version of the Visual Studio runner NuGet package (xunit.runner.visualstudio).
For future googlers I had a rare scenario that caused this.
On my base test class I had a property named TestContext. This interfered with MSTest's reserved TestContext property causing all my tests to be hidden from VS/Resharper except one (which did not inherit from the base).
for me it was changing 'solution configurations' to Debug (instead of Release).
My problem was because my unit test method wasn't void, and it was receiving parameters.
I have found that unit test methods marked as async void aren't discovered by the VS Test Explorer. This seems to be because VS would not have any way to wait for a test to finish and decide if it succeeded or not. If you absolutely need to have a test method to run asynchronously then get it to return a Task instead like async Task.
I found that this fixed the issue for me.
Try building all projects as MSIL (Any CPU) instead of x86/x64. Worked for me strangely
While AndyG's solution works, a more lasting solution could be to set the PreferredToolArchitecture environment variable to "x64", either by:
How to make Visual Studio use the native amd64 toolchain
or by:
Control Panel | System and Security | System | Advanced System Settings|Environment Variables
PreferredToolArchitecture = x64
DefaultToolArchitecture = Native64Bit
PROCESSOR_ARCHITECTURE = x64
ProcessorArchitecture = x64
I was facing the same problem and I've remembered, again (this situation happened before), that selecting "Mixed Platform" on the solutions platform menu works, as well as the other answers.
I'd managed to add mine as
public static void TestMethod1(){}
started working once I removed static....
Go to Nuget package manager and download Nunit Adapter as follow.
go to the project menu > Configuration Manager check your test project platform matches the rest of the project and is check to build then rebuild.
Just ran into this as well as I didn't see a similar case that was similar to mine.
In the .csproj file of my test project, NUnit reference privacy was set to False:
<Reference Include="nunit.framework, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
<Private>False</Private>
</Reference>
After I set <Private> to True it worked.
You just need to install this package only:
NUnit TestAdapter
To get tests to show in the Test Explorer Window I had to install NUnit3 Test Adapter 3.0 which was not available in Package Manager.
Downloaded from https://visualstudiogallery.msdn.microsoft.com/0da0f6bd-9bb6-4ae3-87a8-537788622f2d
I had the exact same problem.
It was due to incompatible version of NUnit I'd added to my project (3.2.0) and the Test Adapter I had installed (2.0.0).
To fix, use "Tools > Extensions and Updates" and search for NUnit3 Test Adapter, it discovered my tests after that.
Cheers
Let's just say for argument's sake that you need to use X64 architecture on your test project in order for the dependencies to build properly (as in my case). You may need to modify your Default Processor Architecture under the Test--Test Settings menu. Setting this to X64 allowed my test explorer to find my tests using Microsoft.VisualStudio.TestTools.UnitTesting.
Sorry for adding to the long list, but I had a completely a different problem.
First, I would like to mention I discovered my issue when clicking 'Run All' in the Test Explorer and then watching the build output window in Visual Studio. You have to actively watch it, as afterwards the message disappears.
As for the issue, it looks like during the scanning of the tests, the DLL gets loaded and its test types are enumerated. This causes the references to be loaded and if any failure occurs during this process, the tests will not be shown in the explorer. I had two issues preventing the test DLL to be successfully loaded:
There was still a binding redirect left in the config file (redirecting to a version lower version NHiberate than what was referenced in the test project).
A conflicting assembly reference (2nd level references not being able to load). AsmSpy is btw a great tool to hunt for these.
If you load a Visual Studio (VS 2015 Community in my case) solution from a network share or My Documents directory that is part of a share, you will get into this trouble. I solved it by moving the solution and its underlying projects to a local folder.
After spending 2 days... none of the above worked for me. The only "solution" was: Go to project properties -> Build Tab. Then click Advanced button on the right bottom corner of the pane. Change "Debug Info:" to "full" and click OK.
Here are the screen shots:
I came across the same issue.
And investigated and found that the dll's were not build,
and put in the right folder. as soon as I changed my configuration
they appeared.
- the Projects build options, what folder should be used?
- the build menu entry build configuration, they should be checked.
that fixed it for me.
For the Visual Studio 2013.5, clearing the \TestResults directory in the solution helped. Visual Studio corrupted the mdf file in which it stores the discovered tests, thus preventing the discovery of unit tests.
Be sure your all projects are runing with the same configuration.
Under your project's Properties => Debug => Platform in drop down list choose the appropriate platform (for me it was "Any CPU") as determined at your other projects.
I know that the unit tests are not found if the solution is not built, so that is something to try (Build the solution), but that solution is like the help desk asking if your computer is plugged in...
After a clean rebuild did not fix the problem for me, running a full batch build did fix it.
Had the same issue; tests suddenly stopped being discovered.
Nunit Test Adapter had become disabled somehow. Clicking enable in the extension manager fixed it for me.
I had the same problem until I realized I made a cut/paste error and left off [Test Method] before the test.

running a c# test project through an exe file

I have a bunch of c# tests that I want to run through a powershell script. I am currently doing this by creating a windows application project, writing the tests as a part of a general class and compiling it into .exe and running the exe but its not the right way to go.
How can I, say, create a class (that can come out as .exe) that can run all tests in a given project or some other way that I can easily run all tests through a powershell script, which is used for application deployment
Try to use MSTest.exe. There are lots of parameters
Example from the link above.
mstest /testcontainer:Errors.dll /detail:testtype
or (VS90COMNTOOLS change to your version)
"%VS90COMNTOOLS%\..\IDE\MSTest.exe"/testcontainer:Errors.dll /detail:testtype
or (change 'Microsoft Visual Studio 11.0' to your VS folder)
"%PROGRAMFILES%\Microsoft Visual Studio 11.0\Common7\IDE\MSTest.exe" /testcontainer:Errors.dll /resultsfile:out

Compiling a C# project as a class library and executable at the same time in VC# Express 2008

I am working on a C# WinForms application in VC# 2008 Express, writing unit tests with NUnit 2.5.5, and running them via the NUnit GUI program. Right now to run them I switch the output type to 'class library' and then switch back to 'windows application' after I'm done testing. I just have NUnit reading from the bin/Release directory, which is erased when I rebuild. I would like to be able to compile both the class library and executable with a single action so I can test via NUnit and still run as a windows application.
I was thinking to use the post-build events in VC# but have never used them (I'm new to NUnit as well), is there a way to accomplish this? Should I be doing this a different way? Any suggestions are appreciated!
It is a .NET goodie: the public classes in a EXE can be loaded from it just like a regular class library. There's no need to build it to a DLL.
Why not build your NUnit tests as a separate project within the solution? Simply add the main application as a dependency to the NUnit test project.
If you then set the NUnit module to build as a library, and the main application to build as an executable, you should achieve the desired effect.

Categories

Resources