Moq mocked object is null when running MSTest from command line - unit-testing

A .NET Core MSTest unit test that passes when I run it in Visual Studio is failing when I run it from the command line using the command "dotnet test" (not passing any arguments). I'm getting the error message:
System.NullReferenceException: Object reference not set to an instance of an object
It's failing because a dependency that I've mocked using Moq is null. At first I thought that the issue might have something to do with the test in Visual Studio being run with loose mocks and then for some reason being run with strict mocks from the command line, this turned out to be false as even when I got the test passing in Visual Studio with strict mocks it was still failing from the command line.

Turns out I was being an idiot. The dll I was running against was an outdated file that preceded me changing the name of the test project. Thanks Peter Csala for making me look a bit closer at the unit test that was failing, it made me realise that the failing unit test no longer exists in my code, making me realise the dll was outdated.

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.

Throwin std::exception causes VS 2013 unit testing module crash

I've created unit test project for my C++ application in Visual Studio 2013. Unfortunately, it does not correctly handles exceptions during test: If i run test that just does following:
throw std::runtime_exception("hello!");
I see Windows "application has stopped working" box telling me that vstest.executionengine.exe has crashed. Meanwhile, Test Explorer in visual studio shows that this test has passed without errors.
Visual studio itself reports that "active test" was aborted, and error is unexpected. Reffers to https://msdn.microsoft.com/en-us/library/bb787181(VS.85).aspx but there is absolutely no useful information on that page about my problem.
How to properly terminate test execution in C++?
Solution
Ensure that all your tests can run at one time, because that's how unit tests work in VS2013 (Run All i mean)
Ensure that you have no assert(..) calls, use Assert class provided by header CppUnitTestAssert.h
Try removing I/O from your test code, for example, try using Logger class provided by header CppUnitTestLogger.h instead of your own implementation
Use command TESTS > Debug > Debug all tests to run all tests in debug mode, you will definetely find reason of your crashes.
More information
Reason of crashes
I think it is how assertion worked in my code, maybe it's abort(). This caused something unexpected in hosting process.

Resharper ignores [DeploymentItem] if no build occurs

I have a unit test written in MSTest with a deployment item decorator to copy a configuration file.
The test itself deletes a line from the configuration file and then verifies the delete occurred correctly.
Now here is the rub.
If I run the test again in ReSharper, it refuses to redeploy the item again, instead leaving the previous copy of the file there, now already missing a line, and the test fails.
If I run the same test again, but using MStest, it passes.
It appears MSTest will always honor [DeploymentItem] even when a build does not occur, but ReSharper will only deploy items if an actual build takes place.
Anyone else seen this?
Using Visual Studio 2012 and ReSharper 7.1.3

Debugging unit tests with resharper

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.

Can VS.Net Unit Test be run outside the IDE?

Is there a way you can run your UnitTest project against a compiled DLL outside of the IDE? Basically we have testing procedures to validate code before moving into production.
I would not want to run the tests inside the IDE. I would want to have the compiled code ready to move to production and be able to run a final test against the .dll before the final copy.
Is there some kind of Command-Line utility that could do this? Just supply both .dlls and get a "all good" report of some kind.
I think you're probably looking for [MSTest](http://msdn.microsoft.com/en-us/library/ms182486(VS.80).aspx)
To run all tests in a specific container, you could do something like the following at a command line:
MSTest /testcontainer:MyTestProject\bin\debug\MyTestProject.dll
[MSTest Command Line Options for .NET 2.0](http://msdn.microsoft.com/en-us/library/ms182489(VS.80).aspx)
MSTest Command Line Options for .NET 3.5
Other Stack Overflow related MSTest Questions