I'm attempting to write tests with NUnit3 as part of some tech-debt migrations. I created a new project within an existing solution. Using nuget I added NUnit and NUnit.Console as per the instructions on github. (I also added the NUnit 3 Test Adapter extension to Visual Studio 2015, but I'm fairly sure that has no bearing on my current situation).
After adding the nuget packages I attempted to import the TestFixture attribute, however, visual studio isn't recognizing the NUnit.Framework namespace and I can't import anything.
The only thing I could thing to fix it was to add the reference manually. There too I was blocked by NUnit not being available.
I'm somewhat at a loss as to how to move forward. How do I proceed and fix the missing reference?
Update: The project I created was of type Unit Test Project, however I've gone ahead and create a Console Application and Class Library. I attempted to add NUnit via nuget to each of them and all of them have had the same result.
Update: Other nuget dependencies seem to install correctly with no discernable difference.
This is only a pseudo solution, as I don't know what the actual issue is. If anyone else comes across a better fix than my work-around I'll be glad to select it if it works for me down the line.
At the time of this writing, the latest version is 3.4.1. I forced nuget to downgrade to 3.4.0 for both the packages NUnit and NUnit.Console and violĂ the namespace is available as you'd expect any nuget package.
Related
I'm trying to use NSubstitute in my Unity project (Unity version 2019.2.2f1). No matter how to import it, my IDE, both Visual Studio and JetBrains Rider, would give an error when I try using NSubstitute saying NSubstitute is undefined.
I have tried many options and versions of NSubstitute packages (4.2.1 and 2.0.3). I also import the dll file from net35 folder of the package into a Plugins folder in my Unity project. I also tried to install the same version of NSubstitute using Nuget in the IDE.
I added NSubstitute 4.2.1 (.NET Standard 2.0) in a Unity project, but I needed to include 3 more dependencies for it to work:
system.threading.tasks.extensions.4.5.3
system.runtime.compilerservices.unsafe.4.6.0
castle.core.4.4.0
Update 2020
If you read this in 2020 or later, check the answer from Rodrigo Eleuterio how to make this work with the latest version of NSubstitute.
/end update
I was able to make the 2.0.3 version work with Unity. The newer versions have additional dependencies which I could not make work in Unity, so it seems for now we're stuck with 2.0.3. So here is how I did it:
Download 2.0.3 from the NuGet Website.
Unzip the file and move the lib/net35/NSubstitute.dll into the Plugins folder of your project.
In your test assembly definition, don't forget to reference it, otherwise it will not be found.
When you now reopen the project in Rider it should be found. Sometimes Unity acts up a little and doesn't seem to find the DLL. In this case re-importing the project can help.
The setup is like this:
A Xamarin.Android application, which depends on Android class library
(at least that's what the template is called in VS)
Said class
library, the purpose of which (not entirely relevant, but FYI) is
interfacing with a REST service and has a dependance on the famous
Newtonsoft.Json NuGet package.
An NUnit test project for said
library.
IDE is Visual Studio 2017, latest version.
If you build and deploy the app on the phone, everything is fine.
However, if you try to run tests from the tests project, it says that it can't find the Newtonsoft library.
I've even managed to find a sort of reason: when the library gets built, it's dependancies aren't packed inside, and they are not copied to build directory.
When .apk is built for the phone, the dependancies ARE getting packed inside.
However, when NUnit project builds itself, it only takes the library, and the dependancies are nowhere to be found.
However, there's no interface to control the behaviour of NuGet "Package Reference" type dependencies (blue ones), the properties window is empty for them. And I found no way to add NuGet packages to this kind of project as a ".config" type of dependancy (grey one).
There is a workaround - you can add the Newtonsoft package to the NUnit test project, then it gets copied to the build directory and the Android library works with it, however it doesn't feel right to me. Tests don't need that reference and it has no business in that project.
How to control NuGet packages in Android Class Library?
Your workaround is the correct solution, you don't need to worry too much about it.
That is because the Newtonsoft package is not used directly in your NUnit test project, so Visual Studio / MSBuild doesn't know if your test project needs this Newtonsoft library. In order to avoid reference pollution in NUnit test project, Visual Studio / MSBuild tries to only bring references over into NUnit test project that it detects as being required by project Xamarin.Android application.
So, to resolve this issue, we often add Newtonsoft to the test project or give a copy task to copy it to the test project.
See This Thread for some more details.
Looking for an answer to another question I now found info that my described behaviour is a known problem, described by .NET developers here:
https://github.com/dotnet/standard/issues/481
In my VS2017 solution I have several unit test projects.
In on of them my xUnit tests do not get discovered by mstest and therefore are not run.
I do get the warning:
Warning: [xUnit.net 00:00:00.7641189] Skipping: Testing.UnitTests (could not find dependent assembly 'Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0')
But Microsoft.VisualStudio.QualityTools.UnitTestFramework is referenced in the project in exactly that version.
I have multiple unit testing projects. Many of them contain xUnit tests as well, but they get found and executed without any problems.
All projects containing xUnit tests have the same xUnit nuget packages assigned, especially xunit.runner.visualstudio
I am also using NCrunch and NCrunch has no problem discovering and execution the xUnit tests in question.
What could possibly be the reason to my problems?
Further Findings:
After upgrading Microsoft.VisualStudio.QualityTools.UnitTestingFramework to version 10.1.0.0, the xUnit tests were found but then I suddenly had 438 from 2447 failing. Those failing tests -mostly not xUnit - were previously working fine.
After shutting down an restarting visual studio the UnitTeestingFramework was automatically reset to version 10.0.0
opening the Reference Manager (Add References) all the project references are shown by selected checkboxes except of Microsoft.VisualStudio.QualityTools.UnitTestingFramework which only shows up if I previously add e.g. "Quality" to the search box, but then I see Microsoft.VisualStudio.QualityTools.UnitTestingFramework three times. One checked (version 10.0.0.0) an other one with same version, but different path and one with version 10.1.0.0
All together looks quite odd to me...
I had this issue but am not sure if this is exactly the same as yours. I saw an error in the output/tests window which stated it couldn't restore package xunit.runner.utility 2.2.0
After manually adding this in in package manager console like so:
install-package xunit.runner.utility -v 2.2.0
It started working again
Let's say my solution has 2 projects:
The first called "MainProject" (A .NETStandard 2.0 project).
The second called "MainProjectTests" (A NUnit test project) with some unit tests for each class into "MainProject".
The first project (MainProject) has a NuGet dependency called "dependencyX". Obviously, the project "MainProjectTests" has a reference to "MainProject".
So when the test runner runs a test of "MainProjectTests" that calls methods from "MainProject" using "dependencyX" I'm getting a System.IO.FileNotFoundException exception:
System.IO.FileNotFoundException : Could not load file or assembly
'dependencyX, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one
of its dependencies.
Why am I getting this exception? When I add "dependencyX" to "MainProjectTests" all works fine, but it seems to me not a good practice... How to solve it?
I'm using Visual Studio for Mac Community 7.2 preview (7.2 build 583)
Thanks for the help.
EDIT:
Tried putting the options:
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
in the NUnit project, but getting the same result.
This seems to be a known bug regarding .NET Standard libraries (and maybe especially in conjunction with NUnit). I already filed a bug report here which seems to confirm that this is not the intended behaviour. Altough there has been no progress for over half a year.
Maybe one should file a bug in the NUnit repo after confirming this only happens when using NUnit.
For the time beeing you'll need to reference all libraries used in a .NET Standard project also in all projects referencing the .net standard one as you are doing right now.
It is a bug reported to Microsoft few times and it seems they did not do much on this, look at this Visual Studio does not copy referenced assemblies through the reference hierarchy
On the other hand at least with Nuget Packages you have a simple way (to add the same package to multiple projects in the same solution) using the package manager for the solution, as you can see here Nuget Package Manager.
I am using VS13 Community Edition I want to use Moq in my project. To do so, I went to Nuget Package Manager and searched Moq but no relevant result was there. But I found library named 'Moq: an enjoyable mocking library' and I installed that in my project. But I cannot use it in my project.
Screens
How I installed library
Error
It seems that although you downloaded the NuGet package, something did not succeed as the DLL wasn't added to your references.
Two things you should do:
Make sure it downloaded succesfully
Manually add a reference to Moq.dll.
I suggest looking at the Package Manager Console provided with VS for more detail.
Check you have add the Moq library as a referance in your unit test application.
I think You removed the Moq reference from your project.
From your 2nd image I get their isn't any reference for using Moq.
Please check if you add the reference like below picture.
If you are not able to see it means. Just follow the bellow.
Please note you have to check in you Installed application menu in below link not in online menu as you did in your 1 image.
In your Project please uninstall the Moq library first. like in below picture.
Then you have to install the Moq again from the online menu like below.
After installing the Moq library now you can able to use the Moq Reference.