Unit Test Organization - c#

I'm using Nunit for unit testing, and added another project called "Unit Testing" to my current solution. I referenced Nunit, and changed the Namespace to the same namespace used in the main project.
I can't seem to figure out how to get access to all the classes, files, etc in the main project. Is there something I have to do to link two projects?

Did you add a project reference to your main project? Right click on your Unit Test project --> add reference --> project --> select your Main Project and add it

In your "Unit Testing" project, you just need to add a reference to the main project you want to test, just like you added the reference to NUnit.

You need to add a reference to the project like you would if you were linking any project that were held in the same Solution file.

Related

How to exclude helper projects with "Test" in their name from unit testing?

We have a utility project named Foo.Testing.Common with helper classes for our real test projects, named things like Foo.Tests.Unit and Foo.Tests.Integration. The Common project doesn't have tests, just base classes, utility methods and the like. It references XUnit so I can use Xunit.Abstractions.
How can we avoid seeing errors like the one below when we run all tests, either via .net command line or VS Test Explorer? The below error appears in VS Test Output during test discovery, for example.
No test is available in C:\Foo.Testing.Common\bin\Debug\netcoreapp3.1\Foo.Testing.Common.dll. Make sure that test discoverer & executors are registered and platform & framework version settings are appropriate and try again.
Microsoft.VisualStudio.TestPlatform.ObjectModel.TestPlatformException...
I assume the test process is matching on test to find test projects and then throws an error if one of those projects has no tests. Or maybe it's looking for projects that reference any xunit packages?
We could just rename the project, but is there a different way? Would like to avoid passing a list of specific test projects to the command line just to avoid the "no tests found" warnings. That is, dotnet test should just work.
Update: I've also tried referencing just xunit.abstractions and xunit.extensibility.core rather than the main xunit package, but the Common project still gets recognized as a test project.
Also tried setting IsTestProject to false in the project properties.
In order to skip Foo.Testing.Common you can set IsTestProject property to false in Foo.Testing.Common.csproj:
<PropertyGroup>
<IsTestProject>false</IsTestProject>
</PropertyGroup>

Cannot add Universal project to Unit test project

When i right click to add reference to my Domain project from my unit test project, I get the following error:
Any idea what might be wrong?

Difference between C# Test Project and C# Class library Project

Whats the motivation behind using c# test project rather then c# class library project to hold my unit tests?
Thanks.
The test project will, by default, have all the MSTest references added for you automatically. Also some default examples, such as a simple example test, is created for you.
With the class project, you can build a test project from that too, but you manually have to add the MSTest references yourself. Not a major problem really, but the test project can save you time and hassle.
EDIT:
As noted in the comments, the big difference between the two project types is that, with a class project, you can choose whichever unit testing framework you like.
I wasn't able to find too much information about this Test project subtype identified by {3AC096D0-A1C2-E12C-1390-A8335801FDAB} however, according to https://msdn.microsoft.com/en-us/library/bb166488.aspx project subtypes can
include customizations:
- saving additional data in the project file,
- adding or filtering items in the Add New Item dialog box,
- controlling how assemblies are debugged and deployed,
- and extending the project Property Pages dialog box.
Indeed right-clicking on Test project in context menu -> in "Add" submenu you can see test items "Unit Test", "Ordered Test", "Generic Test". Adding "Unit Test" generates a test class template for MSTest.
Also it contains additional data like:
<TestProjectType>UnitTest</TestProjectType>
<IsCodedUITest>False</IsCodedUITest>
So it seems that this project subtype is rather MSTest specific.
So if not using MSTest (using xUnit for example) I prefer a class library.
Additional argument maybe the fact that in .Net Core project flavoring (subtyping) is not desired way to do things and indeed .Net Core test templates are class libraries. See discussion: https://social.msdn.microsoft.com/Forums/vstudio/en-US/061aaf74-bb13-4646-9d69-064f6f1b8ef6/net-core-project-subtypes
Bonus: xUnit.net.TestGenerator extension generates a Test project subtype if you don't select existing project (probably because it's just an xUnit adapter for VS generation tool)

Adding this project as a reference would cause a circular dependecy

I'm working on an MVC 3 project. I was told to get all the models and viewmodels out of the projects and put them in a class library so that they can be referenced from different types of projects. However, now that I've transferred all the viewmodels and models from the web project to a class library, and removed all the references to the web project, I cannot set reference to the class library from my web project with the reason stated in the question title. WHy is this happening? In my class library I'm not referencing the main project anywhere!!! Any suggestions? Thanks a lot!!
Experienced this earlier. Check the project that you are going to add if it has the reference to the project you are adding in it.
Example: Project A with reference to Project B. Then in Project B, you're adding Project A as reference.
well this usually happens for a reason, and this is that there is a cirrular reference,maybe not a direct one but an indirect one (through third project, how many projects do you have in your solution?).
In your library project remove all other projects references from solution, and try it again.
good luck
almir

Why one project (exe) does not see the namespace of another project (dll) in the same solution?

Why one project (exe) does not see the namespace of another project (dll) in the same solution?
You need to add a reference from the using project to the DLL first.
Select Project|Add Reference, Projects Tab.
A "solution" in Visual Studio is a collection of projects. Each project is independent of all the others. The solution is just a convenient way of organizing projects and opening them all together.
If one project is going to use the public objects defined in another project, then it must be compiled with a reference to the other project. This is true whether the projects are part of a single solution or not.
To signal to Visual Studio that the EXE must be compiled with a reference to the DLL, you must add the DLL to the EXE's list of references in the Solution Explorer.
You need to add a reference to the DLL.
Right-click the EXE project, click Add Reference, go to the Projects tab, and select the DLL.
Also, make sure that the classes in the DLL are public.
Sounds like you need to add a reference to the dll
Right click on the project --> Add Reference

Categories

Resources