Gallio: An exception was thrown while exploring tests: System.Reflection.ReflectionTypeLoadException - unit-testing

I'm able to run my unit tests through VS2010 with the CodeRush but when I try to run the tests with Icarus Test Runner I get this error.
An exception was thrown while exploring tests.
Location: C:\XXX\XXX.Server.Tests\bin\Release\XXX.Server.Tests.DLL
Reference: XXXServer.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null Details: System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
at System.Reflection.Assembly.GetTypes()
at Gallio.Common.Reflection.Impl.NativeAssemblyWrapper.GetTypes()
at Gallio.Framework.Pattern.TestAssemblyPatternAttribute.PopulateChildrenImmediately(IPatternScope assemblyScope, IAssemblyInfo assembly)
at Gallio.Framework.Pattern.TestAssemblyPatternAttribute.Consume(IPatternScope containingScope, ICodeElementInfo codeElement, Boolean skipChildren)
at Gallio.Framework.Pattern.DefaultPatternEvaluator.Consume(IPatternScope containingScope, ICodeElementInfo codeElement, Boolean skipChildren, IPattern defaultPrimaryPattern)
I've made sure Copy Local is set to True for project references.

Tests were being run on a 64bit machine with the test project Platform target set to "Any CPU" while the project being tested was set to "x86". So the machine was loading the tests as 64 which caused the error when trying to load the project being tested the same way.

Related

Nugetserver onpremise unable to run due to newtonsoft.json version

Target set as .net 4.6 in a vs2015 environment. All fetch and built no errors.
Used https://www.hanselman.com/blog/HowToHostYourOwnNuGetServerAndPackageFeed.aspx and MS doco as reference.
When run under IIS, shows error below
How do I resolve this ?
Exception Details: System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.2106.0
NB: Version on the Newtonsoft.Json.dll file is 9.0.1.19813
web.config has these 2 lines, do I need to make this and nugget.dll.config have newVersion="9.0.2.0" say ?
< assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
< bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.1.0" />
I have it working, what I did was change the default project of "nuget" to "nugetwebserver" and adjusted the assemblies and module name.
Then did a fresh compile. I suspect the poor choice of name caused some conflict with the some setting seeing by nuget.server project was called ..
Interestingly the http://server/nugetserver2.7/nuget would render in IE
http://server/nugetserver3.1/nuget asks me if I want to download the content. The content being the same as the rendered page for 2.7..

Intellitest 2015 is failing where as the Unit test is getting passed

I am using Visual Studio 2015 IntelliTest to do the data driven test execution which is very useful tool for data driven testing. Visual Studio 2015 Intelli test is getting failed when I am running the intellitest from a project created by intellitest. But the same is passing when running the test from a test explorer window. We have implemented repository pattern and using Entity Framework 6.0 to carry out the database operations. Please find the stack trace below for more information. Can anyone help me on this?
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Void MyFramework.Persistence.Entity.EF1Repository..ctor(System.String connectionStringName, System.String objContextName)
at System.Void MyFramework.Persistence.PersistenceManager..ctor(System.String schema, System.String module)
at System.Collections.Generic.List`1<MyDTO> BusinessLogic.ListedPassenger.Passengers(MyDTO entity)
at System.Collections.Generic.List`1<MyDTO> BusinessLogic.Tests.ListedPassengerTest.PassengersTest(BusinessLogic.ListedPassenger target, MyDTO entity)
Please ensure that your test is isolated from the environment using appropriate mock implementations (using Fakes, etc.). Take a look here for a hands-on example: https://blogs.msdn.microsoft.com/visualstudioalm/2015/08/14/intellitest-hands-on/.

All unit tests throwing BadImageFormatException with Moq?

I'm currently in the process of increasing code coverage on our software products and have ran into an issue; all of my unit tests (when compiled using 'Any CPU') are failing due to throwing a 'BadImageFormatException'.
This exception can be circumvented by building the solution using 'x86' instead of 'Any CPU', however the requirements are such that we need to be able to run them using Any CPU/x64 bit.
All unit tests involving Moq follow pretty much the same format:
[TestMethod]
public void GetProduct_ValidId_ProductReturned()
{
//Setting up the object
Product prod = new Product();
prod.ID = 7;
prod.Name = "Test";
//Create the mocks
var mockProductRepo = new Mock<IRepository<Product>>();
var testDb = new Mock<IUnitOfWork>();
//Setup what the repo needs to return, in this case it's a Product
mockProductRepo.Setup(m => m.getByID(7)).Returns(prod);
//Setup what the database needs to return, in this case it's our repo which we've already setup
testDb.SetupGet(m => m.ProductRepo).Returns(mockProductRepo.Object);
//Run the test
Product returnedProd = ProductHelper.getProduct(testDb.Object, 7);
Assert.IsNotNull(returnedProd);
}
I can confirm that this test is successful when it is built using x86. Does anyone have any ideas on how I can get Moq to play nice when built using 'Any CPU'?
As an aside I can also confirm that all my projects in the solution are build using the same value ('Any CPU'). I'm using Moq v4.0.
EDIT: Here is the full exception: Test method [ProductName and the method called] threw exception:
System.BadImageFormatException: Could not load file or assembly '[Product name], Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
Ok so after some digging I finally found out what the issue was. Even if you select 'Build' and then 'Configuration Manager' from the toolbar and see that Platform is set to 'Any CPU' (as was my case), what I hadn't done was check the Target Platform in the project.
To check the Target Platform you need to do the following:
Right-click your project and select 'Properties'
Select the 'Build' tab on the left
Ensure that the Target Platform of your test project matches that of the project you are testing
In my case my test was targeting 'Any CPU' but my live project was targeting 'x64'. This is what was causing the issue.
This can be caused by missing project or other assembly references. Try making sure you have project references for all the projects in your solution.
This post has a further example.

Exception discovering xUnit.net tests with Visual Studio Online build server

I am using Xunit.net with Visual Studio Online hosted build. My tests are being discovered and run fine both locally and on the build server. But on the build server I get this exception (causing the build to reach a "Partially Succeeded" state). This is odd, since all my tests are in fact being discovered and run.
[xUnit.net 00:00:01.3170293] Exception discovering tests from C:\a\bin\xunit.runner.visualstudio.testadapter.dll: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualStudio.TestPlatform.ObjectModel, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
File name: 'Microsoft.VisualStudio.TestPlatform.ObjectModel, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' ---> System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualStudio.TestPlatform.ObjectModel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
File name: 'Microsoft.VisualStudio.TestPlatform.ObjectModel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value HKLM\Software\Microsoft\Fusion!EnableLog to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].
at System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)
at System.Reflection.RuntimeAssembly.GetExportedTypes()
at Xunit.Sdk.Executor.EnumerateTests..ctor(Executor executor, Object _handler)$$RethrowMarker$$ at ExceptionExtensions.RethrowWithNoStackTraceLoss(Exception ex)
at Xunit.RemoteAppDomainManager.CreateObjectTObject
at Xunit.Xunit1Executor.EnumerateTests(ICallbackEventHandler handler)
at Xunit.Xunit1.Find(Predicate`1 filter, Boolean includeSourceInformation, IMessageSink messageSink)
at Xunit.Xunit1.Find(Boolean includeSourceInformation, IMessageSink messageSink)
at Xunit.XunitFrontController.Find(Boolean includeSourceInformation, IMessageSink messageSink)
at Xunit.Runner.VisualStudio.TestAdapter.VsTestRunner.GetTests(IEnumerable`1 sources, IMessageLogger logger, XunitVisualStudioSettings settings, Stopwatch stopwatch)
See http://go.microsoft.com/fwlink/?LinkId=254169
I have the following nuget packages installed in the test project:
xunit 1.9.2
xunit.runner.visualstudio 0.99.2
Other unit testing frameworks, such as MS Test and NUnit, are working without problems. This makes me think the issue is with Xunit.net rather than with Visual Studio Online.
I have also opened an issue at the xUnit.net GitHub, but it remains unsolved.
https://github.com/xunit/xunit/issues/47
How can I get this working? Does anyone know of a workaround? Could I somehow suppress the error message?
The testrunner tries to discover the unit tests in xunit.runner.visualstudio.testadapter.dll. Why? Because it matches the default test sources spec of *.test*.dll.
When changing the default test source spec to *.tests.dll or something else more specific, it will work.
Source: http://erictummers.wordpress.com/2014/02/11/execute-xunit-tests-on-hosted-build-controller/

TeamCity NUnit build step failing although all tests pass

I'm setting up TeamCity to run some NUnit tests, TeamCity reports that all the test pass but it reports the build as a fail with the error message 'New build status is : : NUnit error, {build.status.text}'.
Digging in to the build log I can see that there's a failure to load the program under test, here's the reported error;
[Step 2/2] NUnit error running tests in 'C:\TeamCity\buildAgent\work\f73bb9cce36ccd59\ProjectName.Web\ProjectName.BrowserTests\obj\Release\MyProject.BrowserTests.dll' assembly
Loading assembly is compiled for v4.0.30319, MSIL
NUnit runner runtime is v4.0.30319, x86
System.IO.FileNotFoundException: Could not load file or assembly 'MyProject.Browser, Version=0.24.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
File name: 'MyProject.Browser, Version=0.24.0.0, Culture=neutral, PublicKeyToken=null'
Server stack trace:
at System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)
at System.Reflection.RuntimeAssembly.GetExportedTypes()
at NUnit.Core.CoreExtensions.InstallAdhocExtensions(Assembly assembly)
at NUnit.Core.Builders.TestAssemblyBuilder.Load(String path)
at NUnit.Core.Builders.TestAssemblyBuilder.Build(String assemblyName, Boolean autoSuites)
at NUnit.Core.Builders.TestAssemblyBuilder.Build(String assemblyName, String testName, Boolean autoSuites)
at NUnit.Core.TestSuiteBuilder.Build(TestPackage package)
at NUnit.Core.SimpleTestRunner.Load(TestPackage package)
at NUnit.Core.ProxyTestRunner.Load(TestPackage package)
at NUnit.Core.ProxyTestRunner.Load(TestPackage package)
at NUnit.Core.RemoteTestRunner.Load(TestPackage package)
at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Object[]& outArgs)
at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg)
So, why can the file not be loaded I wonder? The tests have all passed so why does this file even need to be loaded now?
I've experimented with setting the .NET runtime to x86 in the NUnit build step, to no avail.
Environment: Windows 7, VS 2012 Update 2, compiling to .NET 4.0, NUnit 2.6.2, TeamCity 7.1.
If this is still active;
It is a pretty clear error:
System.IO.FileNotFoundException: Could not load file or assembly 'MyProject.Browser, Version=0.24.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
You have to create build step to compile tests
Create another build step NUnit and pass path to assembly file you can use wildcards, more info https://stackoverflow.com/a/16629676/1075077 here.