I have installed some specflow packages in .NET Core 3.1
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="SpecFlow.Tools.MsBuild.Generation" Version="3.9.22" />
<PackageReference Include="SpecFlow.xUnit" Version="3.9.22" />
<PackageReference Include="SpecRun.SpecFlow" Version="3.9.7" />
</ItemGroup>
<ItemGroup>
<Folder Include="Hooks\" />
</ItemGroup>
</Project>
But when I build, I got this error:
Severity Code Description Project File Line Suppression State
Error MSB4018 The "GenerateFeatureFileCodeBehindTask" task failed unexpectedly.
System.Exception: Unit test Provider already set.
at TechTalk.SpecFlow.UnitTestProvider.UnitTestProviderConfiguration.UseUnitTestProvider(String unitTestProviderName)
at TechTalk.SpecFlow.xUnit.Generator.SpecFlowPlugin.GeneratorPlugin.Initialize(GeneratorPluginEvents generatorPluginEvents, GeneratorPluginParameters generatorPluginParameters, UnitTestProviderConfiguration unitTestProviderConfiguration) in D:\a\1\s\Plugins\TechTalk.SpecFlow.xUnit.Generator.SpecFlowPlugin\GeneratorPlugin.cs:line 14
SpecRun.SpecFlow and SpecFlow.NUnit can't be installed at the same time in a SpecFlow project. They are used to configure which unit test runner is used.
I assume that you had SpecFlow+ Runner selected, when creating the SpecFlow project. This adds the SpecRun.SpecFlow NuGet package.
And as you add then manually the SpecFlow.NUnit package, you are getting this error.
You can select NUnit in the project wizard, to get from the start a project with NUnit.
This behavior has existed since SpecFlow 3.0.
Docs for Unit Test Provider: https://docs.specflow.org/projects/specflow/en/latest/Installation/Unit-Test-Providers.html
I recently started working with C# and I am working on one of the legacy system we have. I am trying to figure out what is the code coverage for this legacy system. Here is my Sample.UnitTests.csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoFixture.AutoMoq" Version="4.2.1" />
<PackageReference Include="AutoFixture.NUnit3" Version="4.2.1" />
<PackageReference Include="coverlet.msbuild" Version="2.9.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Moq" Version="4.8.2" />
<PackageReference Include="nunit" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="WireMock.Net" Version="1.0.4.17" />
<PackageReference Include="Utf8Json" Version="1.3.7" />
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="System.Buffers" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../Sample/Sample.csproj" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="dotnet-reportgenerator-cli" Version="4.2.10" />
</ItemGroup>
</Project>
I did some research and found out we can use coverlet which can generate cobertura style report. I followed exactly as mentioned here on my mac box and everything works fine and I can see the report being generated correctly on my console and also it generates index.html file which we can use to visualize as well.
dotnet add package coverlet.msbuild
dotnet restore
dotnet build
dotnet test /p:CollectCoverage=true
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:Exclude="[xunit*]\*" /p:CoverletOutput="./TestResults/"
dotnet reportgenerator "-reports:TestResults/coverage.cobertura.xml" "-targetdir:TestResults/html" -reporttypes:HTML;
Now since we use gitlab ci/cd pipeline for our project - Is there any way I can make this part of my .gitlab-ci.yml file so that it can generate report automatically for me whenever build happens and everybody in my team can see it successfully. Since as of now it's all manual as I need to run those above commands on my local mac box and I can see it from my console only by clicking index.html file.
These are my stages of .gitlab-ci.yml file as shown below. If needed I can provide my yml file as well but any simple example where it can demonstrate how can I do this then it will be of great help. I tried searching a lot and couldn't find this at all on how can I do it through gitlab pipeline which uses coverlet and cobertura style report for .net applications..
stages:
- test
- publish
- increment
- deploy
- integrationTests
- release
Can this be done through webhook as well if needed?
May need to use the actual GUID instead of *.
stages:
- test
- publish
- increment
- deploy
- integrationTests
- release
build-and-test:
stage: test
image: mcr.microsoft.com/dotnet/sdk:6.0
script:
- dotnet add package coverlet.msbuild
- dotnet restore
- dotnet build
- 'dotnet test --collect:"XPlat Code Coverage"'
artifacts:
reports:
cobertura: TestResults/*/coverage.cobertura.xml
GitLab can diff with previous Cobertura reports.
If you want HTML instead, simply include it among the artifacts. May also publish it to GitLab Pages.
The other answer didn't work for me because of folder confusion. Coverlet puts the test results in folders relative to the respective unit test projects. So
Either you have to tell gitlab to search for TestResults folders everywhere via wildcards, by setting path to something like ./**/TestResults/**/coverage.cobertura.xml.
Or you provide the --results-directory option (short version -r), to tell dotnet test where to put those files in the first place.
I went for the second option, gathering all results in a cobertura folder in the repo root. Here is a full, valid .gitlab-ci.yml for running tests for merge requests:
image : mcr.microsoft.com/dotnet/sdk:6.0
stages:
- test
test:
stage: test
only:
- merge_requests
script:
- 'dotnet test DotNetSolution
--collect:"XPlat Code Coverage"
-r cobertura'
artifacts:
reports:
coverage_report:
coverage_format: cobertura
path: cobertura/*/coverage.cobertura.xml
I am standing up a NUnit framework in VSCode but am running into an issue when setting up PageFactory within my page ctor. I have added DotNetSeleniumExtras.PageObjects to my .csproj but am unable to call PageFactory.InitElements(driver,this) even with the using SeleniumExtras.PageObjects in place.
I have seen the articles/videos regarding PageObjects being removed from .Net Core but I thought (perhaps naively) that DotNetSeleniumExtras would solve this.
Has anyone gotten this working since PageObjects were pulled out into their own project?
Here are the steps I've followed in getting setup:
1) Install VSCode
2) Create New Folder
3) Open Folder in VSCode
4) Install .Net Core
5) Install Extensions:
C#
.Net Core Test Explorer
Show Test Results (TDD Support for NUnit .Net Core)
6) Add NUnit Template:
dotnet new -i NUnit3.DotNetNew.Template
7) Create a project:
dotnet new nunit
8) Add Packages:
dotnet add package Selenium.Webdriver
dotnet add package Selenium.Support
dotnet add package DotNetSeleniumExtras.PageObjects
dotnet add package NUnit
dotnet add package NUnit3TestAdapter
dotnet add package Microsoft.NET.Test.Sdk
.csproj Contents
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DotNetSeleniumExtras.PageObjects" Version="3.11.0" />
<PackageReference Include="nunit" Version="3.10.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.1" />
<PackageReference Include="Selenium.Support" Version="3.11.0" />
<PackageReference Include="Selenium.Webdriver" Version="3.11.0" />
</ItemGroup>
</Project>
Page.cs
using NUnit.Framework;
using System;
using SeleniumExtras.PageObjects;
using OpenQA.Selenium;
namespace IS.Pages
{
class Landing
{
public Landing()
{
PageFactory.InitElements(DriverUtil.driver,this);
}
// Start Search Button
[FindsBy(How = How.CssSelector, Using = "body > main > header > div.background-wrapper > div > div > div:nth-child(1) > div > a")]
public IWebElement AdvancedSearch {get; set;}
Sigh,
I didn't look at the Dependancies for DotNetSeleniumExtras. Not supported (yet) by .Net Core
I am completely stuck on this issue. So my team has a unit test project in a services test project. The tests are discovered in the test explorer pane however when I try and run the tests I get these errors:
'Multiple test adapters with the same uri
'executor://xunit/VsTestRunner2' were found. Ignoring adapter
'Xunit.Runner.VisualStudio.TestAdapter.VsTestRunner'. Please uninstall
the conflicting adapter(s) to avoid this warning'
'[xUnit.net 00:00:00.0251250] Skipping: (could not find
dependent assembly 'Microsoft.Extensions.DependencyModel,
Version=1.1.0')'
'No test is available in C:\. Make sure that test
discoverer & executors are registered and platform & framework version
settings are appropriate and try again.'
Context information:
Xunit 2.2.0
Visual Studio 15.5.2
Windows 10 1709 Build: 16299.125
My Test Projects project.json:
{
"version": "1.0.0-*",
"testRunner": "xunit",
"dependencies": {
"dotnet-test-xunit": "2.2.0-preview2-build1029",
"Microsoft.AspNetCore.Mvc.ViewFeatures": "1.1.3",
"Microsoft.DiaSymReader": "1.0.8",
"Microsoft.DiaSymReader.Native": "1.4.1",
"Microsoft.Extensions.Logging.Abstractions": "1.1.2",
"Microsoft.Extensions.Testing.Abstractions": "1.0.0-preview2-003121",
"Newtonsoft.Json": "9.0.1",
"WebServices": "1.0.0-*",
"xunit": "2.2.0",
"xunit.abstractions": "2.0.1",
"xunit.assert": "2.2.0",
"xunit.core": "2.2.0",
"xunit.extensibility.core": "2.2.0",
"xunit.extensibility.execution": "2.2.0",
"xunit.runner.utility": "2.2.0"
},
"frameworks": {
"net461": {
"dependencies": {
"Microsoft.NETCore.Platforms": "1.1.0"
}
}
}
The Weird thing is that it works for my team members. But not me. The differences in our environments are: the latest update from Windows and Visual Studio, while they are an update or two behind.
Does anyone know of a workaround?
Installing or updating "xunit.runner.visualstudio" package did the trick for me. Until that, it was not working with framework 462.
I had the same problem and I fixed it by updating Visual Studio 2017 from 15.5.2 to 15.5.4 and updating my test project references from:
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170628-02" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
</ItemGroup>
to:
<ItemGroup>
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
</ItemGroup>
And restarting Visual Studio (apparently VS caches and uses the previous version even after you've updated).
Not sure if one of the 3 things or the combination of them fixed it.
I encountered the same issue, but I have already had Visual Studio 2017 15.5.4 installed. In order to make it work I updated all xunit related references and now my .csproj relevant references look like this:
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
</ItemGroup>
Not really sure what was not working, but my advice is to make sure that these reference are within csproj and have these minimum versions.
I was having a similar issue as yours, stating could not find dependent assembly; when in fact, that assembly was there and available to the xUnit VS runner. However, what the actual issue was, I had Visual Studio set to use the 32bit test runner by default. The 'dependent assembly' it couldn't find was simply a different processor architecture than the xUnit test runner being used.
To fix, as mentioned here, from the Visual Studio menu, go to Test -> Test Settings -> Default Processor Architecture and set that to X64 if you are using 64bit assemblies, or X86 if you are using 32bit assemblies. The error that the xUnit VS runner gives is very misleading to try to debug this issue.
This issue I saw when installing Visual Studio on a new machine. The tests would run on my desktop maching in Visual Studio, but not on my laptop.
Close and reopen Visual Studio works for me.
If you have a project running on the same Visual Studio (or any other instance of Visual Studio), please stop it. Although I know it's awkward, it solved the problem.
If your issue in Visual Studio is that the Test Runner output seems empty, check that the relevant buttons (white boxes, below) are all selected.
I had same issue, I installed below nuget packages
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
Encountered this problem on VS 2019 recently.
Installing VS.QualityTools.UnitTestFramework nuget fixed the issue
I can create a new dotnetcore app that has authentication/identity using the command line:
dotnet new mvc --auth Individual
How can i include entity framework in the project also?
TL;DR
You already have it in your project
Long form answer
After creating your application, it should have Entity Framework as a dependency. I'm assuming that you're running the .NET Core 2.0 SDK.
Here's the output from my machine
$ dotnet new mvc --auth Individual --name testForStackOverflow
The template "ASP.NET Core Web App (Model-View-Controller)" was created successfully.
This template contains technologies from parties other than Microsoft, see https://aka.ms/template-3pn for details.
Processing post-creation actions...
Running 'dotnet restore' on testForStackOverflow/testForStackOverflow.csproj...
Restoring packages for testForStackOverflow/testForStackOverflow.csproj...
Restore completed in 40.17 ms for testForStackOverflow/testForStackOverflow.csproj.
Restore completed in 40.17 ms for testForStackOverflow/testForStackOverflow.csproj.
Restore completed in 25.25 ms for testForStackOverflow/testForStackOverflow.csproj.
Generating MSBuild file testForStackOverflow/obj/testForStackOverflow.csproj.nuget.g.props.
Generating MSBuild file testForStackOverflow/obj/testForStackOverflow.csproj.nuget.g.targets.
Restore completed in 2.84 sec for testForStackOverflow/testForStackOverflow.csproj.
Restore succeeded.
I then look a look at the csproj which was generated:
$ cd testForStackOverflow/
~/testForStackOverflow$ ls
app.db Data Startup.cs
appsettings.Development.json Extensions testForStackOverflow.csproj
appsettings.json Models Views
bower.json obj wwwroot
bundleconfig.json Program.cs
Controllers Services
~/testForStackOverflow$ cat testForStackOverflow.csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<UserSecretsId>aspnet-testForStackOverflow-AD382505-1A70-4A75-8059-1E0E3897A088</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<None Update="app.db" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
</ItemGroup>
</Project>
The important line of the csproj is here:
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
This is a reference to the ASP.NET Core metapackage. This package contains all of the common ASP.NET Core NuGet packages, including Entity Framework Core - as per this screen shot from NuGet (captured a few moments ago)
This means that part of the package restore operation included restoring EF Core into your project.
I would say that you should take a look at this documentation for EF Core - the link should take you directly to the section labelled "The Model". You don't need the stuff in the preceding section (labelled "Get Entity Framework Core") as you already have it.
Of course, if you're using version 1.x of the .NET Core SDK, then it's a slightly different story.