Unhandled MethodAccessException using Moles and xUnit.net to run unit tests - unit-testing

I have a unit test project in Visual Studio 2010 (.NET 4) that utilizes the xUnit.net testing framework, Moq, and the Moles Isolation framework for generating stubs of static methods. I am using xUnit version 1.9 on a 64-bit machine.
To run tests from the command line, I am using the following command:
moles.runner.exe Project.Tests.dll /runner:xunit.console.clr4.exe
However, I get the following exception every time:
instrumenting...started xUnit.net console test runner (64-bit .NET
4.0.30319.1) Copyright (C) 2007-11 Microsoft Corporation.
Unhandled Exception: System.MethodAccessException: Attempt by security
transparent method 'Xunit.ConsoleClient.Program.Main(System.String[])'
to access security critical method
'System.AppDomain.add_UnhandledException(System.UnhandledExceptionEventHandler)'
failed. at Xunit.ConsoleClient.Program.Main(String[] args) at
Microsoft.Moles.Runner.MolesRunner.RunnerRunner.Run(String runner,
String[] args) at
Microsoft.Moles.Runner.MolesRunner.RunnerRunner.Run(String runner,
String[] args) at
Microsoft.Moles.Runner.MolesRunner.LaunchRunnerEntryPoint(MolesRunnerOptions
options) at Microsoft.Moles.Runner.MolesRunner.RunnerMain(String[]
args) at Microsoft.Moles.Runner.Program.Main(String[] args)
It looks like the exception is coming from xUnit; however, I am able to run the tests using xunit.console.clr4.exe alone without issue. It only fails when using the xUnit console from the Moles runner.
I found this on a forum post:
In the .NET 4 framework, security tranparency rules prevent any
security transparent code from calling into security critical code.
What can I check to determine the cause of this error? Is there a security setting I need to change to prevent this?
Note: I am also having the same exact problem on a 32-bit workstation.
Update: I decided to download the code from http://xunit.codeplex.com/SourceControl/changeset/changes/600246119dca and debug myself. In the xunit.console project (the output of which is the exe getting invoked from the Moles runner), the main thread of execution looks like this:
[STAThread]
public static int Main(string[] args)
{
Console.WriteLine("xUnit.net console test runner ({0}-bit .NET {1})",
IntPtr.Size * 8, Environment.Version);
Console.WriteLine("Copyright (C) 2007-11 Microsoft Corporation.");
if (args.Length == 0 || args[0] == "/?")
{
PrintUsage();
return -1;
}
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
try
{
CommandLine commandLine = CommandLine.Parse(args);
int failCount = RunProject(commandLine.Project,
commandLine.TeamCity, commandLine.Silent);
if (commandLine.Wait)
{
Console.WriteLine();
Console.Write("Press any key to continue...");
Console.ReadKey();
Console.WriteLine();
}
return failCount;
}
catch (ArgumentException ex)
{
Console.WriteLine();
Console.WriteLine("error: {0}", ex.Message);
return -1;
}
catch (BadImageFormatException ex)
{
Console.WriteLine();
Console.WriteLine("{0}", ex.Message);
return -1;
}
}
When I run my tests while debugging the code, everything works fine, as expected (since I never had issues running tests from xUnit alone). I noticed the following line, which appears to be where the exception is thrown based on the error message and stack trace from my original post:
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
I commented out this line, built xunit.console.exe, and tried using it as the /runner argument when executing the Moles runner again. This time, no exception was thrown.
I am still at a loss as to why a Security exception is being thrown on this line when invoked from moles.runner.exe, but not when I run the xUnit console by itself.

Compiling from the latest source code from CodePlex runs without the MethodAccessException.

Related

Cannot find element for dotnet test id: error in Resharper 2016.3

I just downloaded Resharper 2016.3 EAP 4 to check out the Unit test functionality with .NET Core. But when I run all unit tests, I get this error:
Cannot find element for dotnet test id:
MvcMovieTests.SimpleTests.TestMethodPassing
Cannot find element for dotnet test id:
MvcMovieTests.SimpleTests.TestMethodFailing
Here are my simple unit tests:
[TestClass]
public class SimpleTests
{
[TestMethod]
public void TestMethodPassing()
{
Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsTrue(true);
}
[TestMethod]
public void TestMethodFailing()
{
Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsTrue(false);
}
}
When I run all unit tests with the MSTEST Test Explorer, they run properly and I see the results. But with Resharper 2016.3 I get the two errors above showing up in the Unit Test Sessions window in Visual Studio 2015 Community.
Clearing up the ReSharper caches fixed it up for me. Open the Environment | General page of ReSharper options. Click Clear caches. More information here.

NUnit TestCaseAttribute causing AmbiguousMatchException with NUnit VS Adapter

I wrote a bunch of unit tests that utilized the TestCaseAttribute. These tests run great on my local machine when I use the ReSharper unit test runner. Unfortunately, when I run the tests through Visual Studio with the NUnit VS Adapter 2.0.0.0 I get the following output:
------ Run test started ------
NUnit VS Adapter 2.0.0.0 executing tests is started
Loading tests from D:\Projects\Ever\WebApp\Ever.UnitTests\bin\Debug\Ever.UnitTests.dll
Exception System.Reflection.AmbiguousMatchException,
Exception thrown executing tests in
D:\Projects\Ever\WebApp\Ever.UnitTests\bin\Debug\Ever.UnitTests.dll
NUnit VS Adapter 2.0.0.0 executing tests is finished
========== Run test finished: 0 run (0:00:00.8290488) ==========
We use the Visual Studio Online hosted build server for our build, and that relies on the test adapter to run our NUnit unit tests. This means I need to figure out a way to make this work with the attribute (much preferred) or I have to work around this limitation.
Do I have to abandon the use of the TestCaseAttribute because MSTest doesn't support parameterized tests1,2?
After further debuging and testing I've concluded that the TestCaseAttribute isn't the cause of the issue. I'm answering my own question instead of deleting1 in case anyone else falls into the same trap that I did.
The TestCaseAttribute works properly as you can see with the following tests. These tests run perfectly well via the VS Test Adapter and the ReSharper test runner.
[TestFixture]
public class SimpleReproAttempt
{
[Test]
[TestCase(true, false)]
[TestCase(false, true)]
public void DoesNotReproduceIssue(bool a, bool b)
{
Assert.IsTrue(a || b);
}
[Test]
[TestCase("", false, true)]
[TestCase(null, true, false)]
public void DoesNotReproduceIssue(string a, bool b, bool c)
{
Assert.IsTrue(b || c);
Assert.IsNullOrEmpty(a);
}
}
The issue seems to be present only in tests that have an overloaded method with at least one of the overloads using async/await.
1: Editing my question based on this information would turn this into a chameleon question, and a chameleon via self answer is discouraged so I dismissed that option as well.

How to run tests in Nemerle project

How do I run tests which will test my nemerle code. So for example, I've a Calculator class and a CalculatorTests class in a nemerle project. I have already added a reference to nunit using package manager ("install-package nunit"). Now NUnit is available in nemerle project.
After writing following code
[TestFixture]
class CalculatorTests
{
[Test]
MyTest() : void
{
def result = Calculator().Add( 1 );
Assert.AreEqual( 2, result );
}
}
I tried to use TestDriven.net visual studio add-in to run the test but couldn't able to. Can someone tell me how to run tests in nemerle or do i have to write code to run all tests when executing a console app?
Maybe it caused by NUnit runs under .Net 2.0 runtime. Try to set a runtime version in the NUnit command line.

Unit Testing for Windows Phone 7 - App does not launch correctly

I am developing a Wp7-App and I want to start Unit Testing. I used the Template from Visual Studio 2010 to create a Windows Phone 7.1 UnitTest-Project and I added the required Assemblies via Nu-Manager.
I can't start the project in emulator or on a real device. I get a blank loading-screen and this error message:
A first chance exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll
Is this a known error? Is there a workaround?
Thanks!
The standard VS Unit test template won't work for WP7. You should look at these links:
http://channel9.msdn.com/Events/MIX/MIX10/CL59
and
http://www.jeff.wilcox.name/2011/06/updated-ut-mango-bits/
I have a Silverlight 4 Test Project using the Jeff Wilcox assemblies and it works fine for my WP7 tests. I use [TestClass] and [TestMethod] attributes and these namespaces inside my tests:
using Microsoft.Silverlight.Testing; using
Microsoft.VisualStudio.TestTools.UnitTesting;
Within the App.xaml.cs file there is minimal code and the following starts it all off:
private void Application_Startup(object sender, StartupEventArgs e)
{
RootVisual = UnitTestSystem.CreateTestPage();
}

Problem running tests under Netbeans

I am running some Junit tests and Netbeans behaves strangely giving the report in "Output" window:
Testcase: warning(junit.framework.TestSuite$1): FAILED
No tests found in uk.ac.cam.ch.wwmm.chemicaltagger.ChemistryPOSTaggerTest
junit.framework.AssertionFailedError: No tests found in uk.ac.cam.ch.wwmm.chemicaltagger.ChemistryPOSTaggerTest
Test uk.ac.cam.ch.wwmm.chemicaltagger.ChemistryPOSTaggerTest FAILED (crashed)
test:
BUILD SUCCESSFUL (total time: 12 seconds)
The (5) tests are there. I have run mvn test which runs them but fails on OutOfMemoryError. Is this likely to be the cause of the Netbeans problem?
How did you created your test file? Manually, or using NB wizard? (Tools - Create JUnit test from Java file's popup menu)
If you are using JUnit 3, all test methods in your test file must start with "test", e.g.
public void testFoo() { //some testing here :) }
With JUnit 4, a '#Test' annotation is required, e.g.
#Test
public void myOwnTestFoo() { //...}
Otherwise JUnit does not recognize the test and throws AssertionFailedError error.