XUnit Tests No Longer Working After Upgrade From .NET 5 To .NET 6 (Q & A) - xunit

I have an xUnit unit testing project. All was working fine in .NET 5.
In Visual Studio 2022, I changed the .NET Version in the csproj to .NET 6 as shown below.
Following this, the tests build but no longer run.
There is no info in the output window. The Test Explorer just says it wont run the tests, with 'unexpected error detected'.
What should I do to fix this?

I created a new project and compared the csproj file with my old project. There were a couple of differences
First, I needed to add the nuget package for Microsoft.NET.Test.Sdk (in my case, version 16.11.0). This seemed to fix it.
I also noticed a couple of properties in the first PropertyGroup that I was missing. I added those too. However, just adding Microsoft.NET.Test.Sdk fixed it for me.

In my case I was changing from NUnit to XUnit and also needed to add the xunit.runner.visualstudio nuget package

Related

How to Configure Specflow 3.0.199 with NUnit 3.11?

I have done this a couple of times before, but it's not working today. Am I missing something?
I want to configure Specflow from the scract, using NUnit and to execute inside Visual Studio.
I've seen many tutorials but they are not working to me :P. I'm trying to use the latest versions.
These are the steps I'm doing on Visual Studio 2017:
Create a test project (.NET Framework)
Install Specflow plugin for Visual Studio (Tools > Extensions and Updates)
Delete reference of MSTests from the nuget packages.
Install SpecFlow 3.0.199
Install NUnit 3.11
Install SpecRun.Runner 3.0.284
After creating a default feature file and generate its steps, when I compile the solution I get this error on CalculatorFeature.feature.cs (the generated file):
It's like those configurations are not compatible. What's going on?
If you have another step by step list, let me know how to configure Specflow with NUnit to run on Visual Studio 2017, please.
For SpecFlow 3 you have to use the MSBuild generation.
To this, follow these two steps:
Add the NuGet package SpecFlow.Tools.MsBuild.Generation with the same version as SpecFlow to your project
Remove all SpecFlowSingleFileGenerator custom tool entries from your feature files.
From https://specflow.org/2019/generating-code-behind-files-using-msbuild/
Background what is happening:
The VS Extension has sometimes problems to find the used SpecFlow version. In that case, it falls back to the SpecFlow version shipped with the extension (which is really old). This version is generating code with now not existing NUnit attributes.
TestFixtureSetUp and TestFixtureTearDown attributes were deprecated for quite some time and were finally removed. They are replaced by OneTimeSetUp and OneTimeTearDown.
Your choices are probably...
Go back to an NUnit version that supports the old attributes.
Get a version of SpecFlow that uses the new attributes.
Find a way to configure SpecFlow and tell it to use the new attributes. Sorry, but this option, which is no doubt the best, is out of my wheelhouse.

How to resolve Lock file error? "...contains msbuild projects but there is no export file"

I am running visual studio Community 2015 Update 3. I have a solution that has been compiling fine but is now generating an error when I try to compile it. The error is
Lock file Microsoft.DotNet.ProjectModel.Graph.LockFile contains msbuild projects but there is no export file
This error is displayed in the Error List window when I compile the solution and the error list indicates that it relates to a project in the solution which is an .Net Core Library that houses my xUnit tests.
The error information indicates that the error is on line 262 of C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\DotNet\Microsoft.DotNet.Common.Targets
Screenshot of that location selected below:
The solution contains:
____Asp.Net Core project targeting net461
____Windows Library Project targeting .Net Framework
____ A 2nd Windows Library Project targeting .Net Framework 4.61
____A .Net Core Libary Project targeting net461 (used holding XUnit Tests)
If I try to compile each project individually, they all compile fine except for the project holding the xUnit Tests that generates this error.
The Big Question
So what does "Lock file Microsoft.DotNet.ProjectModel.Graph.LockFile contains msbuild projects but there is no export file" mean? And how can I fix it?
Posting how I got past this error in case it helps others. Ultimately this issue was solved by removing all the projects from the solution, adding them back one by one and then reestablishing the project references. I would attribute the root issue to the .net core preview2 tooling still being flaky.
I had a similar problem, not with the build inside Visual Studio, but with msbuild. For a long time I was sure that the error was caused by dotnet tooling beeing in a preview state, but finally came upon, that one xproj-project wasn't build before another xproj-project, that was dependent of the first one. So I started looking for solutions to build-order problems instead, and came across an old blog-post by Victor Sergienko
http://victorsergienko.com/project-dependencies-of-visual-studio-2008-broken-in-msbuild/
that solved the problem by adding a dependency from the second project to the first like this:
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ProjectOne", "ProjectOne.xproj", "{CAF36C07-36C7-4842-A2DA-9737005D2835}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ProjectTwo", "ProjectTwo.xproj", "{82875D1E-3F13-430B-8946-C26E08BD9DF9}"
ProjectSection(ProjectDependencies) = postProject
{CAF36C07-36C7-4842-A2DA-9737005D2835} = {CAF36C07-36C7-4842-A2DA-9737005D2835}
EndProjectSection
EndProject
Now my solution builds in TeamCity again... finally!

Visual Studio FsUnit test setup - Exception NUnit.Engine.NUnitEngineException

I am using Visual Studio 2013, installed FsUnit 2.2.0, which requires NUnit 3.2.1 and FSharpCore 3.1. I created a separate test project and put a testfixture and test in there. My platform is x64 Win 10. The config is for 'AnyCPU' and 'Debug'. I've tried test settings for x86 and x64. When trying to build and create tests, I get:
------ Discover test started ------
NUnit Adapter 3.2.0.0: Test discovery starting
Exception NUnit.Engine.NUnitEngineException, Exception thrown discovering tests in C:\Users\Dad\Documents\Visual Studio 2013\Projects\...
Cannot run tests in process - a 32 bit process is required.
Exception NUnit.Engine.NUnitEngineException, Exception thrown discovering tests in C:\Users\Dad\Documents\Visual Studio 2013\Projects\...
Cannot run tests in process - a 32 bit process is required.
NUnit Adapter 3.2.0.0: Test discovery complete
========== Discover test finished: 0 found (0:00:00.1230077) ==========
If I change the config for both the test and target projects to 'x86' (instead of AnyCPU) then the error for the base project goes away, but the same thing happens for the test project.
No tests are ever discovered, help please ... and many thanks!
You will need to remove nunit from your solution and install it again with "NUnit3TestAdapter", version 3.0.10 works.
Uninstall any NUnit software in add/remove program and in your solutions. Now using Nuget package manager (tools > NuGet Package Manager > Manage NuGet Packages for solution...) remove any NUnit you have in a solution and install older version (e.g. 3.0.1). Find "NUnit3TestAdapter" and install in version 3.0.10.
I had the same problem with my .NET Core 2.0 project with NUnit 3.9 in Visual Studio 2017 and tests were not showing up in TestExplorer. Was stuck with this for quite some time. None of the solutions suggested in other related questions worked.
Then I figured out from this link that a class library with target .NET Standard does not work. The test project has to target .NET Core. Also, Microsoft.NET.Test.Sdk NuGet is required.
So, the steps are
Make sure that the test project targets .NET Core
Install latest NUnit NuGet (I used 3.9)
Install corresponding NUnitAdapter NuGet (I used NUnit3Adapter)
Install Microsoft.NET.Test.Sdk NuGet
Re-build and your tests will appear in Test Explorer in Visual Studio.
Note: Already added this answer to another .NET Core specific question. Adding here too, as the solution might be helpful in this scenario as well.
There is an error in NUnit 3.2.1 whereby the TestEngine assumes that it can't run a test requiring 32-bit execution in process. The assumption is always valid for NUnit's own runners, but not necessarily when the process is started by some other program. There's an issue filed about this.

An exception occurred while invoking executor 'executor://mstestadapter/v1': Object reference not set to an instance of an object

We are using Visual Studio 2012 and Team Foundation Server 2010 in our project. Since, we are using Microsoft Fakes, we could have not used the started Test functionality of our Team Build 2010, I have created a build activity which invokes VSTest.Console.exe passing the test assemblies. This approach has been working well for us. However, after a recent check-in we are getting the following error when executing Vstest.console.exe
Error: An exception occurred while invoking executor 'executor://mstestadapter/v1': Object reference not set to an instance of an object.
I copied the binary files to my local machine and was able to get the error again. Interestingly, we run a dev build before check-in which runs the same vstest.console.exe with the same parameters and that works correctly. I have got two folders on my machine, for one the call works fine and for other it doesn't. The compiled assemblies look same size and there aren't much differences to suggest.
I tried using Process Monitor but couldn't find anything obvious other than that the process returns with an exit code 1. Does anybody know much about this error? Is there any way I can find out more information from my test run?
Regards,
Hamid
We got to the bottom of the problem. We are using Entity Framework 5.0 and we also have a situation where some of our test assemblies are targeting .Net 4.0 and some are targeting .Net 4.5.
When we are trying to run the tests such that assemblies targeting different versions of .Net framework and using Entity Framework 5.0 are run in the same Test (vstest.console.exe) process, we get this error.
I think this has to do with the way Entity Framework 5.0 ships with two versions (net40 and net45). Once one version of the library is loaded in the process, the other version cannot be loaded and this caused the error.
I had the same error message just now with Visual Studio 2017 using the new MSTest.Framework and MSTest.TestAdapter (to be able to use Live Unit Testing). It turned out I was using multiple versions of those packages in my solution. Consolidating them to the latest version solved the problem.
This is what worked for me:
In Visual Studio, Test -> Test Settings -> Default Processor Architecture -> X64.
Though it might not solve everyone's issue, hope it helps some of you out there.
I have the same error specified in the subject when in the AssemblyInfo of the project is specified the AssemblyAttribute:
[assembly: AssemblyCulture("en")]
If this attribute is present, the run of the unit tests fails.
If I remove this line of code or I remove the en language, the run of the unit tests success.
An exception occurred while invoking executor 'executor://mstestadapter/v2'
I had multiple TestAdapter's and TestFramework's in my BuildAgents Folder, deleting the v1.1.11 versions fixed the issue:
This happened to me recently in VS 2019. Using NuGet to update to the latest versions of MSTest.TestAdapter and MSTest.TestFramework solved the problem for me.
I tried everything on the web to solve this exception. Finally I solved this issue by installing the Update v4 of Visual Studio 2013 (which is that I'm using currently). Hope it helps!
Deleting app.config and packages.config, if exists, and reinstalling Entity framework via nuGet packages works for me.

NUnit isn't running Visual Studio 2010 code

I'm trying to load a Visual Studio 2010 beta dll into the NUnit GUI. I get a popup error.
This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded. You may be attempting to load an assembly build with a later version of the CLR than the version under which NUnit is currently running.
How do I force an executable to run under .NET 4?
I've downloaded the NUnit 2.5 source and opened the VS2008 solution in the VS2010 beta. Once the conversion finished I opened all the projects and changed the target framework setting for all the projects to ".NET Framework 4.0". I then built the solution without any errors. I can now use the NUnit GUI app to run tests built for .NET 4.0. I've not done exhaustive testing of this build so there may be problems, but for my purposes it works fine.
Update: It is not necessary to rebuild NUnit. I discovered that if you add the following to the relevant NUnit config file you can run a test dll built for .NET 4.0.
Under <configuration> add:
<startup>
<supportedRuntime version="v4.0.30319" />
</startup>
and under <runtime> add:
<loadFromRemoteSources enabled="true" />
With .NET 4 being released, I used
<supportedRuntime version="v4.0.30319" />
in the NUnit 2.5.4 exe.config instead of requiredRuntime, and the loadFromRemoteResources tag as shown above and all worked well. Thanks!
You don't have to modify any file
just open this file and everything will work just fine
C:\Program Files (x86)\NUnit 2.5.10\bin\net-2.0\nunit-x86.exe
In NUnit 2.5.5 you can specify in the command line the option /framework=net-4.0 and it is compatible with the new assemblies generated with the release of the latest VS2010.
The proposed solution worked great for getting NUnit to run. Unfortunately, when I then got to my code coverage step, NCover started giving me:
Profiled process terminated. Profiler
connection not established.
The best solution I found to this was to just use the "-x86" version of NUnit with NCover:
NCover.Console.exe
nunit-console-x86.exe --additional params--
Works now.
To find your .net 4 version from the Visual [C# 2010 Express, or which ever] go to the Visual application's "About" under the menu's Help item. It should show up as the version under the Microsoft .NET Framework line on the top right hand side of the window.
I ran into the very same error message while running NUnit 2.4.8. As I had not upgraded in some time I installed the current NUnit (v2.5.9) and found that it now supports VS2010 assemblies. So if you have newly encountered this error check your NUnit version: as of December 2010 (or so) the only thing you need to do is upgrade NUnit.
As of NUnit 2.5.10 you can enable visual studio support in the GUI runner:
Tools-> Settings-> IDE Support
After that I was able to successfully attach to the nunit-agent.exe process which runs your assembly in a .NET 4.0 app domain
If you experience this issue after upgrading to nunit 2.5.5 then you will need to upgrade nant to the latest version for me it was .91 alpha.
I found usefull to start from NUnit Application Template. It support VS C# Express, allows debugging tests and contains precompiled NUnit for .NET 4.0. Thank to author new test project gets ready with one click.

Resources