Visual Studio unit test not working and not showing tests - visual-studio

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! :(

Related

Unit Test can't see friend class variables

I'm converting Google tests to Visual Studio Native unit tests. I'm running into an issue where the Visual Studio unit test isn't seeing variables of it's friend class. Any idea how to fix this?
In my VSUT-Br.cpp class (ported from Google unit test):
#include "DocFriendClass.h
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace VSUTBr
{
TEST_CLASS(BrTest)
{
public:
protected:
virtual ~BrTest();
virtual void SetUp();
virtual void TearDown();
std::unique_ptr<DocFriendClass> TestDoc;
...
TEST_METHOD_INITIALIZE(File_Initialize)
{
pP = CreateP();
TestDoc = std::make_unique<DocFriendClass>(OurString("Test"));
TestDoc->m_Parser.setParser(pP); //it can't see m_Parser either
...
OurString sColor(kszColor);//it's not seeing kszColor in friend
}
};
}
In my DocFriendClass.h:
...
private:
Parser m_Parser;
...
#ifdef UNIT_TEST
friend class BrTest;
#endif
Then I went to my VSUT-Br project and right clicked and selected properties, and edited the C/C++ Preprocessor settings; Preprocessor Definitions now include UNIT_TEST=1. I clean built and it still couldn't find the friend variable. I exited VS and went back in, and clean built and it's not finding the friend variable. It worked having that as a friend with the old Google test. Any ideas?
I looked at unit test friend but it's not the same problem. I found elsewhere and they were saying I can't have a friend to my VS Unit Test, but I have a feeling that's internet garbage (can't find link now).
Update: I added some info on a variable, m_Parser in DocFriendClass that it's not seeing. Error info:
C2248 'DocFriendClass::m_Parser': cannot access private member declared in class 'DocFriendClass'. File VSUT-Br.
It should be able to access this. The Google test could as a friend.
We wound up working around the issue by making the private variables we were accessing public. Then it didn't need to be a friend anymore. (they just shared nicely)

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!

Nunit not running SetUp method in Visual Studio debug mode

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();
}

Debugging Nunit Tests inside VS2010 Express

I have written a bunch of unit tests inside VS2010 Express and tests being tests they sometimes fail. Since the express editions of VS don't allow plugins to run I can't simply spin up TestDriven.Net or an equivalent and debug the tests. To try and work around this I've converted my test assembly into a console app and made the main method look like this:
class CrappyHackToDebugUnitTestInVSExpress
{
public static void Main()
{
AppDomain.CurrentDomain.ExecuteAssemblyByName(
#"C:\Program Files\NUnit 2.5.5\bin\net-2.0\nunit-console.exe",
new [] { Assembly.GetExecutingAssembly().Location, "/framework:4.0" });
}
}
In theory I should be able to run this up, set break points in my test. If it worked it would be an acceptable work around, but I keep getting the following:
FileLoadException
Could not load file or assembly 'C:\\Program Files\\NUnit 2.5.5\\bin\\net-2.0\\nunit-console.exe'
or one of its dependencies. The given assembly name or codebase was invalid.
(Exception from HRESULT: 0x80131047)
Now the file exists and when run manually nunit-console runs fine. What might be my problem?
Basically you need to convert your assembly to Windows Forms app, add reference to the nunit-gui-runner.dll assembly and change your Main method to look like this:
[STAThread]
static void Main()
{
NUnit.Gui.AppEntry.Main(new string[] { Assembly.GetExecutingAssembly().Location });
}
here is another example:
...
using NUnit.Gui;
namespace __libs
{
class Program
{
[STAThread]
static void Main(string[] args)
{
NUnit.Gui.AppEntry.Main(new string[] { #"C:\test\bin\Debug\test.exe" });
}
}
}
This will allow you to step into certain tests but is not very good for a red green cycle, so you will want to use this only when debugging and not in other circumstances.
I played with your concept and it appears the issue isn't directly from loading the file, but from dependencies.
I used the following modified code:
And the error was actually a failure to locate nunit.core.dll, which is in the /lib directory.
try
{
String NUnitPath = #"C:\Program Files\NUnit 2.5.7\bin\net-2.0\nunit-console.exe";
AssemblyName asmName = System.Reflection.AssemblyName.GetAssemblyName(NUnitPath);
AppDomain.CurrentDomain.ExecuteAssemblyByName(asmName, new[] { Assembly.GetExecutingAssembly().Location, "/framework:4.0" });
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
Trace.WriteLine(ex.StackTrace);
}
(I like getting System.Reflection.AssemblyName because you can inspect and see that everything's in order verses the raw file path.)
A quick bulk copy (xcopy nunit.*.dll) into my test projects' debug directory and it ran just fine. (It should be trivial to discover the minimal dependencies required)
Tested in VC# 2010 Express with NUnit 2.5.7 (breakpoints work, but I didn't really play with any other options.) Although I'm sure you could make a passable build option from it.
Cheers!
PS - First post here so I'm a bit untested as to getting the 'code' blocks formatted. Sorry in advance..
I had a similar problem trying to debug unit tests in VS C# express. Had a hard time getting it to work properly but then I found out about this project template. Works perfectly in C# Express!
http://visualstudiogallery.msdn.microsoft.com/b8a7a8fa-9f5a-4b9b-8e8b-8839a4364f26?SRC=VSIDE
C# Project Template
Integrated tests with Visual Studio, including Visual C# Express
version
Self contained NUnit console runner. Allow to write test fixtures and
test, running from Visual Studio simply by pressing F5 (support test
debugging), or Ctrl-F5 free run with results in console window. In
case of test failure indicate by beep sound.
Contains essential NUnit modules to start test project. No external
dependencies. Simply create new project, using NUnit Test Application
template.

Running MSIL inside Visual Studio

We have Visual Studio 2005.
I can successfully run MSIL programs from the command line using "ilasm".
I would like to be able to run these inside VS.
From other posts and searches, I gather you need to create a "Console Application" type of project? (They allude to the fact that VS can handle MSIL but I can't find any specific "how to".)
"Console Application" gives the standard Program.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace AddNumbers
{
class Program
{
static void Main(string[] args)
{
}
}
}
I then add my .il file (which adds two numbers) to the project. The project builds successfully and when I run it it does nothing and the breakpoints that I set in the .il file are not triggered. Not surprising because the .il file is not being called.
Can some please provide a step-by-step approach to getting this to work?
I'd also like to know what namespace I need to add to the .il file and also if it is possible to pass parameters to the .il file and (if so) how?
You would need some kind of ILASM MSBuild task.
You would need to enable the line numbers and debugging options for ILASM.
And here is a complete example, courtesy of MSDN.

Resources