dotnet test does not recognize xunit tests - xunit

I know that this question has been asked multiple times before I raise it again, However I still could not get the answer for dotnet 5.0 and xunit
What have I tried.
I have the following test defined
public class IntegrationTests
{
[Theory]
[MemberData(nameof(Tests), MemberType = typeof(IntegrationTests))]
public void Test(IntegrationTest test)
{
Assert.True(test.Expected, test.Actual);
}
}
Visual Studio 2019 recognizes all the tests and runs them without any issues
dotnet test command says
dotnet test <path to>.csproj
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
No test is available in C:\<path>\bin\Debug\net5.0\<projectname>.dll.
Make sure that test discoverer & executors are registered and platform & framework
version settings are appropriate and try again.
I don't fully undertand what "test discoverer and executors mean here.
My .csproj file has following nuget packages (since many of the similar questions got resolved by adding one of these)
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.console" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>

I was working on a windows machine and I did the following to make it work
Closed all my vscode / visual studio instances
Deleted C:\Users\<username>\.nuget\packages folder
Deleted references for testrunner packages in <projectname>.csproj
and it started picking up the tests again

Related

How does NUGET package management works on Microsoft MAUI?

There are significant differences compared to Xamarin and I couldn't find docs or even a discussion about the matter, and I am interested in knowing. It was pretty straight forward in Xamarin. If I wanted a nuget package for the android project and not the whole solution I could do that, but now it's just one project.
You could specify the name and version of the package in .csproj file and set the certain platform with property Condition .
For example
If you just want to add the package to single platform
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" Condition="'$(TargetFramework)' == 'net6.0-ios'" />
</ItemGroup>
If you want to add the package to multiple platforms
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" Condition="'$(TargetFramework)' == 'net6.0-ios' or '$(TargetFramework)' == 'net6.0-android'" />
</ItemGroup>
PS : Firstly make sure the package support net6.0 and net6.0 - xx before you add the package .

Running EF Core migrations does not find environment variable in VS Code

Started building up an API solution with Azure Functions with runtime version 3.x and .NET Core 3.1. In the solution using also Entity Framework Core 3.1 with code first approach.
Check the .csproj file:
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
<RootNamespace>project_name</RootNamespace>
<PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.3" />
</ItemGroup>
In the StartUp class I have the following to create the database context:
public RequestContext CreateDbContext(string[] args)
{
string sqlConnectionString = Environment.GetEnvironmentVariable("SqlConnectionString");
var optionsBuilder = new DbContextOptionsBuilder<RequestContext>();
optionsBuilder.UseSqlServer(sqlConnectionString);
return new RequestContext(optionsBuilder.Options);
}
So in the project I have my models for migrations and running the command with .NET Core CLI as the following:
dotnet-ef migrations add InitDb
Meanwhile running the Azure Function locally there are no issues, it can read the connection string value from local.settings.json file without any issues. Once running the migrations command I get the following error:
Value cannot be null. (Parameter 'connectionString')
Question:
So it's clear it cannot find the environment variable called SqlConnectionString once running the migrations command. Using Visual Studio 2019 with Package Manager Console there is a way to solve this issue by setting up the environment variable as the following command before running migrations:
$env:SqlConnectionString="Server=<connection-string-value>"
It's pretty annoying to switch between PMC and VS Code just because of this step if I want to run migrations. I was checking different answers in similar topics but none of them helped me that much.
So is there any way to do the same and setup environment variable with VS Code terminal using bash? Or what could be the possible solution for this scenario?
Thank you!
The problem was I originally tried to create an environment variable without the export keyword. So with the following command in bash dotnet migrations was leading the Value cannot be null error:
SqlConnectionString="Server=<connection-string-value>"
Based on Jim Xu's comment I created an environment variable which seems to solve the issue as:
export SqlConnectionString="Server=<connection-string-value>"
See the full log:
$ export SqlConnectionString="Server=<connection-string-value>"
$ echo $SqlConnectionString
Server=<connection-string-value>
$ dotnet-ef migrations add InitDb
Build started...
Build succeeded.
Done. To undo this action, use 'ef migrations remove'
The references which were helping the solution were:
Environment variable vs Shell variable, what's the difference?.
Also you can find further information in the SO link: Defining a variable with or without export
Thanks for sharing that link!

Nuget version mismatch for Microsoft.AspNetCore.Mvc.ViewFeatures when using metapackage

I migrated an ASP.NET Core project from .NET Framework to .NET Core and in the process replaced the AspNetCore nuget packages with the Microsoft.AspNetCore.App metapackage. Originally I specified a version for it which worked fine, but VS whined about that telling me I'm not supposed to specify a version, so I removed it. Once I did that this happened:
'Microsoft.AspNetCore.Mvc.ViewFeatures, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' which has a higher version than referenced assembly 'Microsoft.AspNetCore.Mvc.ViewFeatures' with identity 'Microsoft.AspNetCore.Mvc.ViewFeatures, Version=2.0.0.0,
These are coming from a Unit Test project which references my ASP.NET Core project. The ASP.NET Core project builds fine on its own. Now, I understand what this error means - somewhere in this Test project I must be referencing this package with a different version than what is provided by the metapackage. What I want to know is where is this package being referenced in my Test project? I have the following nuget packages in the Test project:
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.2.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="Moq" Version="4.10.1" />
<PackageReference Include="MSTest.TestAdapter" Version="1.3.2" />
<PackageReference Include="MSTest.TestFramework" Version="1.3.2" />
</ItemGroup>
I don't see where any of these packages use any AspNetCore dependencies, so I'm at a loss as to where the version mismatch could be.

ASP.NET Core (NET Framework) Teamcity build fails, VS builds properly

I have an ASP.NET Core project that builds properly with VS but fails with TeamCity.
It is a project that compiles to a library, but TeamCity tries to build it as an executable, and complains about the lack of 'main':
CSC error CS5001: Program does not contain a static 'Main' method suitable for an entry point
The content of the .csproj file are as follow:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup Label="Configuration" Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputType>Library</OutputType>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net462</TargetFramework>
<RuntimeIdentifier>win7-x86</RuntimeIdentifier>
<OutputTypeEx>library</OutputTypeEx>
<StartupObject />
<AssemblyName>Test</AssemblyName>
<RootNamespace>Test</RootNamespace>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
</ItemGroup>
</Project>
Visual studio has no problem building the dll file.
To reproduce:
Create an ASP.NET Core (.NET Framework) project
Change the output type to library
Remove the program.cs / startup.cs files
Compile with Visual Studio to confirm a library is being built
Build with Team City and an error will appear
To avoid that error, Please look into this SO post or this
You should be using the dotnet core plugin or you can easily
configure dotnet build command(if dotnet is present in your build
servers).
Or you can refer the MusicStore build.cmd file for reference. This basically downloads and installs the dotnet and all the dependencies and then builds the project.
Hope it helps!
I found a workaround; in the project file, VS puts this:
<OutputTypeEx>library</OutputTypeEx>
I need to add one line:
<OutputType>Library</OutputType>
<OutputTypeEx>library</OutputTypeEx>
So it looks like the build with TeamCity is not handling the OutputTypeEx propery but it handles the OutputType one.
I still see this as a bug, but at least there is a workaround.

Cannot modify an evaluated object originating in an imported file

I am trying to delete a file from my visual studio project, but this dialogue pops up and prevent me from doing that. What does it mean and how do I resolve this problem?
There is a xamarin thread about this but no solution there.
https://forums.xamarin.com/discussion/25719/cannot-modify-an-evaluated-object-originating-in-an-imported-file
I ran across this question as well as a couple others related to the error message, specifically. My issue was different from the OP's as it had to do with NuGet packages not installing/uninstalling/updating.
I opened the .CSProj files only to see the package reference completely missing for Microsoft.NET.Test.Sdk, ie some VS file somewhere was caching this bad reference, even after killing all existing .vs directories. So I just added it manually, ran nuget restore, and was back in business.
Basically, I modified this block of project file XML:
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
<Version>6.2.8</Version>
</PackageReference>
<PackageReference Include="MSTest.TestAdapter">
<Version>1.4.0</Version>
</PackageReference>
<PackageReference Include="MSTest.TestFramework">
<Version>1.4.0</Version>
</PackageReference>
</ItemGroup>
to this:
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
<Version>6.2.8</Version>
</PackageReference>
<PackageReference Include="MSTest.TestAdapter">
<Version>1.4.0</Version>
</PackageReference>
<PackageReference Include="MSTest.TestFramework">
<Version>1.4.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk">
<Version>16.1.1</Version>
</PackageReference>
</ItemGroup>
Restarting Visual Studio fixed the issue for me.
We have no idea what the real problem is, but we do find a fix by comparing this project with some other project files that we have.
The fix is to open the .Shared.vcxitems file in an text editor and replace this:
<ItemGroup>
<ProjectCapability Include="$(MSBuildThisFileDirectory)SourceItemsFromImports" />
</ItemGroup>
with this
<ItemGroup>
<ProjectCapability Include="SourceItemsFromImports" />
</ItemGroup>
In my solution happend because there was NuGet packages source pointing to non-existent local folder, changed it in Package Manager Settings.
My workaround was to open the .vcxitems and .vcxitems.filters, mentioned in the error message, in text editor and delete the file directly in the xml. Also I want to note that this error appears for me only when I tried to delete files that were part of different .vcxitems that was imported into main project. You can check your main project for <Import Project="xyz.vcxitems" />.

Resources