Nunit not running SetUp method in Visual Studio debug mode - visual-studio-2010

I'm trying to debug into the tests after the setup method is called and the tests depend on the setup method being called.
I'm using Nunit 2.6.0.12051 testing a .Net 4.0 class library. The nunit Visual Studio project has a class marked with [SetUpFixture] and a method marked with [SetUp].
If I run the tests from the NUnit gui, I'm fairly certain the setup attrib'd class is called (because it isn't stopped at the setup class with a run-time error now) but I can't debug into it. If I try to change the settings to see Verbose Tracing, NUnit gui throws an unhandled excption.
If I run the tests from Visual Studio via Test View/Debug Selection, the break point at the setup method doesn't stop execution and the trace statements inside the method don't print in the debug window. So I'm certain the setup method isn't getting called.
While I could just change the setup class to be the base of all test classes, I only need the method run once.
Any help would be wonderful.

I just ran into this issue and eventually found this significant sentence from the NUnit SetUpFixture documentation:
"This is the attribute that marks a class that contains the one-time setup or teardown methods for all the test fixtures under a given namespace."
Turned out my SetUpFixture class was in an entirely different namespace than my tests, so it wasn't being run.

I just noticed the same when using the latest NUnit from NuGet (2.6). The [Setup] method is not run before the [Test] -methods.
I don't know why they changed this quite significant part of NUnit, but I fixed it for my purposes by going back to version 2.5.10 which does run [Setup] before [Test].

I had this issue too but installing the latest version of the test runner (TestDriven.NET in my case) fixed it. It wasn't an NUnit issue for me.

I just ran into a similar issue. My unit test was not calling setup either. After reading the NUnit doc. referred in the top answer, I figured my solution was extremely simple.
In very simple terms, I was missing the "Setup". Realised after reading the doc.
*Example from NUnit SetUpFixture doc mentioned in top answer.
[SetUp]
RunBeforeAnyTests()
{
//
}
----------------------------------------------------
// this was my fixed up code
// simply added [Setup] and boom, bob is your uncle.
[SetUp]
public void Setup()
{
readChoice.Reset();
}

Related

Visual Studio unit test not working and not showing tests

I am following MSDocs guide to put up some unit testing. I've never used them before and I've never been using them because to me they are totally useless, but I see it's a common practice, so I wanted to learn more.
Obviously, this is not working.
I've put up some .NET Standard 2.0 libraries with little to no code to do some log4net logging. I've addet a "Unit test project" as the guide suggests and added a method decorated with the "TestMethod" attribute and all the other attributes.
Test explorer windows does not show any test and I'm not able to even run them in any way. What am I missing?
Here's the code
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MrLogger_netstandard;
namespace UnitTests
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void ErrorLogOnFile_Default() => MrLogger.Instance.FileErrorLog("I'M AN ERROR!!! :)");
}
}
The MrLogger class it's just a class that does logger.Debug("Write message"), a classical log4net logging call.
I've tried this too and it changes nothing.
Jeez... help
For anyone wondering, I had to start visual studio as administrator and then it worked.... UGH! :(

How to order test cases in xUnit

I have the exact case as in this post:
How to set the test case sequence in XUnit
Which points me to this link:
http://www.bricelam.net/2012/04/xunitnet-extensibility.html
Followed the snippet of code in the post, the one using RunWithAttribute.
But as one of the comments in the post pointed out,
once I annotate my test class with [PrioritizedFixture] all of my test classes are no longer recognized by the xUnit Runner in Visual Studio.
Is there anyway to make this work?
*Using VS2013 and xUnit 1.9.2

Visual studio 2010 is not executing code after including gmock.LIB

Earlier I was using gtest for my project. For the time being I am using gmock and when I have provided the path for gmock.lib, gmock_mock.lib and ..\..\include too. Then the control is not at all going into the code.
Suppose previously it was like e.g.
main()
{
printf("Hello world"); //Kept the breakpoint here, control comes here
}
Now after adding .lib and include paths it is not at all executing just strats debug and ends without going anywhere...
Please help me.
Google test lets you write unit tests using the TEST macro. It is not intended for use with normal programs that already have a main(). If you want to use google test you should create another project.

Visual Studio test project with MSTest tests reports "the project does not contain any tests"--though it does

I have a Visual Studio 2010 solution with several projects, including .NET4 test projects. One such project, when I try to run it without debugging (Ctrl+F5), reports:
"Cannot start test project MyTestProject because the project does not contain any tests."
But, the project has several tests. Each test class is marked with the [TestClass] attribute, and each test method within is marked with the [TestMethod] attribute. Just to demonstrate my own sanity, I added this file and it still wouldn't recognize that there are tests.
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Foo {
[TestClass]
public class UnitTest1 {
[TestMethod]
public void TestMethod1() {
}
}
}
The project is properly set as a test C# project. The tests in question also fail to show up in the Test List Editor's "Tests not in a list" section. (I translated most of the tests from VB.)
Why isn't Visual Studio recognizing the tests? How could I troubleshoot it?
Turns out that these tests were translated to C# from VB; when I translated them, I forgot to add explicit namespaces on them. Thus there were two test classes in the global namespace with the same name, which caused one to effectively replace the other. I just wish the warning given was a little less cryptic!

Use for WorkItemAttribute?

I noticed there's an attribute Microsoft.VisualStudio.TestTools.UnitTesting.WorkItemAttribute available in visual studio testing (I'm using VS 2010 Premium and work items with TFS 2010.)
Marking a test method with a work item number sounds handy, but does it actually do anything? I can't tell if there's any tool support for it at all. I set one up like this:
[WorkItem(25788)]
[TestMethod]
public void TestSomethingSpecificToABug()
{
...
But no magic - I thought maybe the context menu on the test in the Test Results window might offer to open the work item, or Team Explorer might have a feature to search for tests. The MSDN documentation is no help either. What is this attribute good for?
According to "Software Testing with Visual Studio® 2010" by Jeff Levinson (Addison-Wesley Professional, February 2011, ISBN-10: 0-321-73448-3):
This also means that one existing property should not be used anymore:
Associated Work Items. This value is not reported to the data
warehouse and therefore cannot be used for reporting. If you currently
use this property, consider associating your test with an actual Test
Case work item type.
So the answer is, don't use this with TFS 2010.
WorkItem Test Method attribute is not used for associating test methods to test cases. It is typically used to associate a test method with a bug.
A related C# example from Code Index - How to discover ignored tests:
When using MSTest to build your suite of unit tests, you can use the
attribute [Ignore] to tell the MSTest engine not to run a test instead
of commenting it. You may also use the attribute [WorkItem(id)] to
link the unit test to a bug database (such as TFS) item, so that you
can trace why a particular test has been marked as ignored:
[Ignore]
[WorkItem(12345)] // bug 12345 describes why this test was ignored
[TestMethod]
public void IgnoredButWithWorkItemTest()
{
//The actual code is not important;
}
It's to link the unit test back to a work item in TFS. I'd provide a link to more info but it seems it really is poorly documented.
I haven't used it myself but believe it can be used to generate reports on the status of work items.
This isn´t needed anymore: in VS 2013 via CodeLens
Find linked work items (Alt + 7)
Find linked code reviews (Alt + 8)
Find linked bugs (Alt + 9)
To review a test's definition, double-click the test.
Oh! for those that cherish Lync:
Contact the owner of an item (Shift + F10)
I really do remember, that I used this Attribute before and the test results have been attached to the respective WorkItem.
However, with Visual Studio 2012, it does not work anymore, or I forgot which mechanism was actually responsible for the magic. Could it be that this only works through the build server?
Pulling attributes from the test binary is really useful when you have a home grown test harness built for running Selenium UI unit tests.
After a test failure, I can pull the WorkItemAttribute value using System.Reflection.MemberInfo.CustomAttributes and then look up the ID with the TFS API. If the work item is a bug and it's still active, I can auto-resolve the failure to that bug. In this manner, I can run the failing test every day and automatically resolve the failure to reduce randomization.

Resources