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)
Related
I have a C# .NET 5.0 project with a unit test project.
In my test project I have two namespace for two different kind of tests:
myproject.UnitTest.TestA
myproject.UnitTest.TestB
I want myproject.UnitTest.TestA executed automatically when solution is compiled.
I know that I can do this for both myproject.UnitTest.TestA and myproject.UnitTest.TestB by test option, and I know that I can split test project in other two different projects, but just for general knowledge: it's possible to configure only test on myproject.UnitTest.TestA to be executed when solution is compiled? If yes, how?
BR
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>
We have different C# Visual Studio solutions in different Git repositories. Each repository contains source code that needs functional tests and these tests will be integrated with Azure DevOps. The idea is to have a single C# testing automation framework with generic steps, hooks and logic that can be used among all solutions.
Some ideas I came up with:
Having a separate repository with automation framework files and just copy and paste hooks, steps, configurations files, etc. for each of the solutions / repositories.
Create a SpecFlow project inside of each solution / repository and maintain one automation framework for each solution.
Use NuGet to pack the testing framework and install it into each of the solutions. I personally like this last approach but I am not sure how it can be achieved.
Any suggestions or ideas on how to solve it?
You can use Bindings from External Assemblies, which allows you to create a general class library containing your step definitions and hooks, and then reuse them in multiple test projects.
If using SpecFlow.json in your test project, add this:
{
... other JSON settings for SpecFlow ...
"stepAssemblies": [
{
"assembly": "NameOfYourSharedBindings"
}
]
}
If using App.config:
<specFlow>
<!-- other SpecFlow settings -->
<stepAssemblies>
<stepAssembly assembly="NameOfYourSharedBindings" />
</stepAssemblies>
</specFlow>
Just replace NameOfYourSharedBindings with the file name of the DLL file containing your shared steps and hooks. Be sure to add a project reference to your test project so it references the class library project containing your shared steps.
You are not limited to step definitions in another project inside the same Visual Studio solution. You can reference pre-compiled DLL files as well as NuGet packages. So, really, options 2 or 3 would work for you. Just pick which one makes the most sense for your use case.
I would like to use MS VS2012's add as link functionality, meant for files, but instead for a certain block of code.
I've got a solution with lots of projects. I am creating a unit testing project that will house all the algorithms that exist in the other projects in the solution. I can copy over all the algorithms I want to test into a file in the new unit testing project, however I am also looking for a way to automatically update the code in the test file if say the code in the other projects updates. It is almost as if I want to create a reference to a code chunk in VS.
If no such functionality exists is there some sort of script I could create that updates the code in the test project every time I build?
EDIT:The reason I can not have testing code within the projects themselves is because the other projects in the solution are .NET Microframework projects and the .NET Microframework does not support the use of c# attributes which are being used with the NUnit testing framework. Hence the reason I can not have any test code within those projects. However, there are some algorithms in the driver files of the .Net MF projects that I would like to be able to test and these algorithms are independent of the project type, so I am looking for a way to keep this code in sync so that if any changes are made to the algorithm within the .Net MF projects the same respective change is made within the unit testing project without the need for manual copying.
EDIT: In the simplest terminology all I am looking for is some sort of script I can run to copy over certain code blocks from one project file to another project file.
I had a very similar problem where different parts of the same source code file were shared between different projects. Here is a fix that worked for me.
Suppose the source file File1.cs contains four methods, two of which are used by ProjA and two are used by ProjB, where ProjA and ProjB are different projects inside a Visual Studio solution.
To keep just one copy of File1.cs, use conditional compile symbols, e.g. PROJ_A and PROJ_B for the two projects. Put the methods used by ProjA under conditional compilation symbol #if PROJ_A and put the methods used by ProjB under conditional compilation symbol #if PROJ_B. Then add File1.cs as linked file to both projects and make sure that corresponding conditional compilation symbols are set on those projects.
If this is what you were looking for, let me know if you get any problems implementing it.
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.