nunit TestContext throws NullReferenceException - c#

I have the following code:
[TestFixture]
public class LexicalTests
{
[Test]
public void LexicalTest1()
{
TestContext.CurrentContext.TestDirectory;
}
}
CurrentContext throws an exception while attempting to get TestDirectory or WorkingDirectory property.
How can I solve this problem?
P.S.: On my home PC tests work perfectly (without strange exceptions).

It seems that some applications that offer the functionality to run NUnit unit tests have a problem with the TestContext class.
The test in class below should pass:
using NUnit.Framework;
namespace UnitTests
{
[TestFixture]
public class UnitTests
{
[Test]
public void CurrentContextTest()
{
Assert.IsNotNull(TestContext.CurrentContext);
Assert.IsNotNull(TestContext.CurrentContext.TestDirectory);
Assert.IsNotNull(TestContext.CurrentContext.WorkDirectory);
}
}
}
If the test doesn't pass then, as Dmitry wrote in his comment above, change the NUnit version in the ReSharper menu. From within Visual Studio, go to ReSharper -> Options -> Tools -> NUnit. Click the Specified NUnit installation radio button and ensure that a folder with nunit.core.dll, nunit.core.interfaces.dll and nunit.util.dll is specified. An error will be displayed if the listed files cannot be found.
Once the NUnit version has been changed, re-run the test and it should pass.

Related

Blank output for an ASP.NET NUnit test application

I am currently working with NUnit to write test cases for an ASP.NET application.
I have created a sample test project and have added NUnit latest version (3.6.1) and NUnitTestAdapter 2.1.1.
If I run the application by clicking on menu Test → Windows → Text Explorer, I am getting a blank screen in the Output Window. Why?
My code is:
[TestFixture]
public class SampleTest
{
[Test]
public void StringCheck()
{
string str = "Hello";
Assert.That(str, Is.EqualTo("Hello"));
}
[Test]
public void EmptyCheck()
{
string str = "siva";
Assert.That(str, Is.EqualTo(string.Empty));
}
[Test]
public void NumberCheck()
{
int i = 0;
Assert.That(i, Is.EqualTo(0));
}
}
You are using the adapter for the NUnit 2.x series, rather than the NUnit 3 adapter. NUnit versions 2 and 3 differ substantially, and there are two separate Visual Studio adapters to run the tests.
This is the one you'll need to run NUnit 3.6.1 tests:
NUnit 3 Test Adapter
I found the solution. The problem was with NUnit version (3.6.1). Now I am updated with NUnit 2.6.4. It is working for me.

How do you make internals visible to nCrunch

MyProject has an internal class and method as such.
namespace MyProject
{
internal class InboundMailAlertParser
{
internal InboundMailAlert ParseMessageBody(string body)
{
...
}
}
}
I'm using InternalsVisibleTo in my MyProject AssemblyInfo.cs to expose internal classes/methods to a unit test assembly.
[assembly: InternalsVisibleTo("MyProject.Tests")]
My Test looks like this
namespace MyProject.Tests
{
[TestClass]
public class InboundMailAlertParserTests
{
[TestMethod]
public void ParsesBody()
{
InboundMailAlertParser parser = new InboundMailAlertParser();
parser.ParseMessageBody(messageBody.ToString());
}
}
}
My test passes fine when run in Visual Studio, but nCrunch is failing to build the unit test project due to not being able to see the internal InboundMailAlertParser of the MyProject assembly under test. Is there another nCrunch assembly I have to indicate should have internal visiblity to allow nCrunch to be able to build the test assembly?
Turns out I had ignored a component (project) in the nCrunch config. Once I enabled that, the project could build correctly and I got my lovely green dots.

How do you nunit TestFixture in same cs file as project?

In class they taught us to add a test fixture to the same namespace as the project we're testing. For example:
namespace Project
{
class Decrypt : Cipher
{
public string Execute()
{
//Code here
}
}
[TestFixture]
{
[Test]
public void test1()
{
//Code here
}
}
}
I noticed in the c# menu on my uni computer, there was a 'Test' section (I couldn't get it to run there either, I don't know how). On this old 32b computer there isn't. I've installed NUnit-2.6.2.msi but when I try to run it, it says "Unable to find a version of runtime to run this application"
So I think I have two problems:
installing Nunit (I already have referenced the .dlls from my project separately)
using Nunit (even on a computer with it installed properly)
Normally you'd put your code in separate projects, but reference the project you're testing in the test project
//project: Xarian.Security
//file: Decrypt.cs
namespace Xarian.Security
{
class Decrypt : Cipher
{
public string Execute()
{
//Code here
}
}
}
.
//project: Xarian.Security.Test
//file: DecryptTest.cs
using System;
using NUnit.Framework;
//as we're already in the Xarian.Security namespace, no need
//to reference it in code. However the DLL needs to be referenced
//(Solution Explorer, Xarian.Security.Test, References, right click,
//Add Reference, Projects, Xarian.Security)
namespace Xarian.Security
{
[TestFixture]
class DecryptTest
{
[Test]
public void test()
{
//Code here
Cipher cipher = new Decrypt("&^%&^&*&*()%%&**&&^%$^&$%^*^%&*(");
string result = cipher.Execute();
Assert.AreEqual(string, "I'm Decrypted Successfully");
}
}
}
Right click on the test project's references, go to the Projects tab and select the main project. Once referenced you'll be able to use the classes (etc) from your main project in your test code.

Nunit - TestContext.CurrentContext.Test not working

I am using nunit 2.5.9.10348 and trying to extract the current test name in the TearDown event so I can assign a screengrab filename the test name however it is always null (see the attached image). The private _context variable does have the TestName however this is no use to me!
Has anyone had success using this new TestContext functionality (from 2.5.7).
From your screenshot I see that _context has keys "TestName" and "Properties". But TestAdapter looks for keys "Test.Name" for Name and "Test.Properties" for Properties. So, there is something wrong with TestContext initialization (I think wrong data was put to Remoting.Messaging.CallContext).
After a little investigation (see comments):
NUnit tests should run by NUnit testig environment for Context to be available.
I had the same issue. It occurred when in a TearDown method I executed method, which actually was to make the teardown
[TearDown]
public void CleanUp()
{
TestContext.CurrentContext.Test.FullName; //!=null
someClassInstance.DoTearDown();
}
class SomeClass
{
public void DoTearDown()
{
TestContext.CurrentContext.Test.FullName; //==null
}
}
I have no idea why, but it seemed so. Is it your case?
UPDATE: Now I looked at the screenshot, so it's not your case :)
Same issue with R# test runner. Just downloaded NUnit sources and added a workaround in TestAdapter to make it work with r#
public string Name
{
get
{
return (_context["Test.Name"] ?? _context["TestName"]) as string;
}
}

resharper unit test inheritance

Has anyone got a strategy for unit testing heiarchies in Resharper?
I typically use both TestDriven.Net and Resharper's test runner, with NUnit tests. TestDriven is awesome for everything but quickly finding a bad test out of a batch run (which could be thousands), which is where Resharper's runner comes in.
I typically use a pattern with an abstract base class (like the code below) of test cases overridden to get the right subclass, which works great in TestDriven, but Resharper just ignores them! I had thought as of v5.0 Resharper was using NUnit's code base, which means this should work but it doesn't.
Cheers,
Berryl
[TestFixture]
public class AdminAccountTests : AccountTests
{
protected override Account _GetAccount() { return new AdminAccount(_idScheme, _description); }
}
[TestFixture]
public class LeaveTimeAccountTests : AccountTests
{
protected override Account _GetAccount() { return new LeaveTimeAccount(_idScheme, _description); }
}
public abstract class AccountTests
{
protected abstract Account _GetAccount();
[SetUp]
public void SetUp()
{
_account = _GetAccount();
}
[Test]
public void OnCreation_Blah() {
Assert.That(_account.IdScheme, Is.EqualTo(_idScheme));
}
}
Make your abstract class a TestFixture. I do this same thing with R#.
EDIT: I just noticed that R# (I'm using 5.1 with NUnit 2.6) will mark a class as a test fixture if it has Tests in it, regardless of whether the subclass or the base class are attributed with TestFixture. So that may not solve your problem.
I have the same issue with MbUnit and Gallio with resharper 5.1.3000.12. If i try to launch the test through visual studio plugin, the test is ignored. With external gallio test runner, it works fine.
JetBrains ReSharper 5.1 C# Edition
Build 5.1.3000.12 on 2011-01-28T05:05:56
Plugins:
1. “Gallio Test Runner” v3.2.0.0 by Gallio
Visual Studio 9.0.30729.1.
Copyright © 2003–2011 JetBrains s.r.o.. All rights reserved.

Categories

Resources