Debugging unit tests with resharper - unit-testing

I've just started using resharper and I found a very annoying thing about it. When debugging a unit test, I try to step-into (F11) a method, but visual studio complains that source code is not available. The problem is that resharper is wrapping method calls with its own classes. Of course I can put a braeakpoint further in my source, but this is very annoying. Do you know if there´s a solution for this? By the way, I'm using NUnit to write my tests
Thanks
Federico

After some playing around I managed to recreate this on my setup (Xunit.net in resharper, visual studio 2008)... The steps I took to recreate this are:
Set the dll that contains the tests as the startup project (I know you don't need to do this, was just trying to get it to fail)
Put a breakpoint in the unit test
Push F11 (the shortcut key for 'step into' in my configuration)
This complains 'Cannot start test project 'RowanBeach.Crm.Domain.Test' because the project does not contain any tests.'
Run the 'Debug Unit Tests' command from the Resharper menu (I have this bound to a key combination on my machine)
This displays the 'No source code available...' message
Of course that's not how you should start unit tests from resharper! :) If that's what you were doing (or something similar), try this instead:
Don't bother setting the dll as the startup project - it doesn't need to be
build or rebuild the dll that contains the tests
set the breakpoint
Put the cursor somewhere in the source code for the unit test you want to debug to set it as the 'current context' unit test
Run the 'Debug Unit Tests' command from the Resharper menu (You might want to bind this to a key combination if you haven't already)
Hopefully, that should work

What type of tests are you running - NUnit? MS Test? Something else?
I've never found it difficult to debug tests using R# - just put a breakpoint in your test and go. What method are you trying to step into? I've generally not tried to step into the NUnit methods themselves (assertions etc) but stepping into your own code should be okay.
If you can come up with a quick example, I'd be happy to try it myself.

Related

How to run Google tests (C++ unit tests) in a QNX Virtual machine, using resharper?

Currently how I run my unit tests:
I have a visual studio project, which builds a C++ google test (unit test) executable.
I then copy this exe to my Virtual Machine with QNX and run it manually, to get the unit test results.
Can I use resharper to automate this?
Resharper is able to detect and list tests from test file. But on running them, it throws error as it is not able to execute the tests in Windows.
Below is the exact error, I get, on running any of the tests.
ReSharper Ultimate – Error while running unit tests:
Invalid path
Can I set up resharper and Visual studio environment, so that, I can run tests directly from visual studio, which will copy the exe to the Virtual Machine and give me the execution results?
This will avoid me manually copy pasting the exe and running command to execute the tests?
I searched a lot regarding this in google. But couldn't find much help.
I'm new to google test and resharper. So any help would be greatly appreciated.
Thanks in advance.
No, I'm afraid ReSharper cannot do that out of the box.
Though it is quite powerful, so if you manage to write a script that does the VM specific part, ReSharper should be able to cope with it. What that script needs to do is:
It must be run on the host (Windows).
Copy the test binary to the QNX system in the VM.
Execute the test.
Capture the test's output and redirect it to the host's stdout.
The ReSharper options can be set here:
Menu ReSharper > Options...
In the left pane scroll down to Tools > Unit Testing > C++ Tests
In the text field Command: you need to enter the path to the script.
See https://www.jetbrains.com/help/resharper/Reference_Options_Tools_Unit_Testing_CPP_Tests.html for more details.
I think you'll find that the msg box showing "ReSharper Ultimate – Error while running unit tests: Invalid path" has been caused by the last update to VS 2017, which has broken things. I usually run my google tests through resharper c++, and until a few days ago, it worked. Now, I see this exact same error.

Is there a way to get the information from MS unit test framework outside of the VS environment?

I created a test framework to test a sample application. This basically required creating a Solution for my sample application and then adding a Native Test Project, which can test the application.
The test project generates an .exe, but if I were to execute the .exe on it's own, it does nothing.
Is there a way to get information from the test programme so it can be used for other purposes outside of the VS environment?
Yes, you can run unit tests without VS2013.
Use Run automated tests from the command line using MSTest source to do so.
In general it says:
Open a Visual Studio command prompt.
Either change directory to your solution folder or, when you run the MSTest.exe program in step 3, specify a full or relative path to the metadata file or to the test container.
Run the MSTest.exe program.
I hope it will solve your issue.
It took a couple of days, but I've found that DeJaVo's answer is incorrect. Because I am running a Native Test Project, MSTest will not work with it. To run a Native Test from the command line, use VSTest.Console.

Is it possible to run a unit test in debug mode?

I've fixed a null reference problem. Now when I run the test related to that class, it fails. I'd like to know whether is possible to run a test in debug mode so I'll know why I am getting the exception?
The test was written by some else, it's calling a method, as part of the setup, with a lot of logic in it. I'd like to step in to see how values are being returned.
Thank for helping.
It is indeed possible, using the NUnit Test Adapter for Visual Studio 2012.
Also, just so you're aware: failed assertions will throw exceptions, causing the debugger to break execution. You can adjust your configuration to work around this, as described in this thread:
Debugging in VS2012 with NUnit without throwing exceptions?
You can also use ReSharper, which makes it pretty easy. Or, you can just go into the properties for your DLL project that has the test in it and make the test runner you are using the exe that runs when you run the project.
Right click the project in the solution explorer, select "Properties" from the menu, go to the "Debug" tab, click the "Start external program" radio button then set the project as the active project and run in debug mode.

How to never be prompted to run last successful build when unit testing

I want to get rid of the dialog saying
There were build errors. Would you like to continue and run tests from
the last successful build
when I am running unit tests (I use test -> run -> all tests).
How do I do that? I already know how to disable it when running a normal project.
I also want to know how this can ever be a useful feature?
How do I do that?
You can't.
I also want to know how this can ever be a useful feature?
I find it useful when I am working closely with someone who is configuring test data. I can re-run tests to ensure new test data is valid, without having to worry about compiling any changes that I have done in the meantime.
For example if someone has changed some data in a database, I want to be able to run my tests to ensure that this new data is valid, and I want to be able to run the tests whether the current state of my code compiles or not.
For Visual Studio 2017 / MSTestV2 you can do:
Tools > Options > Project and Solutions > Build and Run
"On Run, when build or deployment errors occur:" "Do not launch"

ReSharper Unit Test Runner: Support for Deployment Items

I like the Unit test runner in ReSharper 4.5, and would like to use it with my MSTest tests, but one thing annoys me:
In some of our solutions, we have set up some Deployment Items in the .testrunconfig file. The ReSharper Unit Test runner does not seem to respect this, so I get errors when trying to run the unit tests from ReSharper.
Is there any workraound for this ?
Update:
citizenmatt's answer was correct, the option to use a .testrunconfig with ReSharper exists in the Options dialog of ReSharper. You have to select the unit test provider on the list, then the controls to do that appears. (That was not obvious or discoverable, at least not for me ;-)
Have you had a look in the Unit Testing options page? There's a setting there to allow you to use a specified test run configuration, or a test run configuration specified in a metadata file. I'm not sure what it means - I've never used it - but it sounds like it might help.
Why not use Post-Build event of your test project? Put all your deployable items in a .BAT (.cmd) file and call it in Post-Build event to get everything deployed.