Adding Xunit for asp.net core application - asp.net

Could you tell me what I am doing wrong?
I have Asp.net core webb application and use VS Code, I tried to add xunit directory for testing my application but finally I got this:
My application cannot see Xunit even when I 'using' it
My algorithm of adding xunit:
Manually create directory UnitTests
Use in command line command 'dotnet new xunit'
And finally I got mistake.

Dit you add the required NuGet packages? Should be present in the project file and look something like:
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />

Related

uno-platform wasm debugging

Still running into wasm debugging. I am on debug and a new project do work but not the project that I stared 8 months ago. I have deleted bin file and obj and user temp files.
<PackageReference Include="Uno.UI.WebAssembly" Version="3.2.0-dev.83" />
<PackageReference Include="Uno.UI.RemoteControl" Version="3.2.0-dev.83" Condition="'$(Configuration)'=='Debug'" />
<PackageReference Include="Uno.Wasm.Bootstrap" Version="1.5.0-dev.57" />
<PackageReference Include="Uno.Wasm.Bootstrap.DevServer" Version="1.5.0-dev.57" PrivateAssets="all" />
It's probably caused by old packages. Following instructions on this page and there should solve your problem:
Update Uno.UI to latest version
Update Uno.WasmBootstrap & Uno.UI.RemoteControl to latest versions
Add reference to Uno.Wasm.Bootstrap.DevServer
Open your .csproj and remove any <DotNetCliToolReference />
It should work after that.

Dotnet publish with .NET core 2.0 and .NET framework projects

I'm hoping someone has some advice on the best way to use Teamcity to build and publish a solution that has both .NET Core/standard 2.0 projects and .NET framework 4.6.x projects in it.
Currently, I can build the project, run tests, but I can't figure out a way to publish it via the dotnet-cli. We have a relatively large solution, approximately 75 projects in .NET core/standard and 5 or some framework projects. Running dotnet publish on our solution results in the following error on the .NET framework projects:
error: C:\Program Files\dotnet\sdk\2.0.3\Microsoft.Common.CurrentVersion.targets(3861,5): error MSB4062: The "Microsoft.Build.Tasks.ResolveManifestFiles" task could not be loaded from the assembly Microsoft.Build.Tasks.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a. Confirm that the declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.
It would be ideal if the cli could attempt to ignore publishing the .NET Framework projects, but it doesn't seem to be possible. I'm thinking about writing a powershell script to check all csproj files in our solution for an appropriate TargetFramework value (i.e netstandard2.0/netcoreapp2.0), and publish them individually, but maybe someone knows a better way?
If anyone is facing the same issue, you need to restructure your csproj file as suggested by #nvoigt.
You can follow the steps as described in the post Old csproj to new csproj
You can start clearing out your csproj file and start with below format.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net462</TargetFramework> // if your target is 4.6.2
</PropertyGroup>
</Project>
And now you can add remaining of your dependency like below.
...
<ItemGroup>
<PackageReference Include="Dapper" Version="2.0.4" />
<PackageReference Include="Microsoft.Azure.Storage.Blob" Version="11.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.0.0" />
<PackageReference Include="Microsoft.Azure.KeyVault" Version="2.0.6" />
<PackageReference Include="NLog" Version="4.7.5" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.6.5" />
</ItemGroup>
...
you can find more details on the post.

NUnit .NET Core running through ReSharper

I am trying to run NUnit tests for a .NET Core 2.0 project in Visual Studio 2017 through ReSharper. I've had the test .proj assembly set both as console application and class library. So it doesn't seem to be the output type. The solution does not discover any unit test and just displays no results with 0 tests run.
Here is my .csproj file for the tests project:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<AssemblyName>eCorp.WebStore.OrderService.Tests</AssemblyName>
<RootNamespace>eCorp.WebStore.OrderService.Tests</RootNamespace>
<ApplicationIcon />
<OutputType>Exe</OutputType>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="NUnit" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.9.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Core\OrderService.Domain\OrderService.Domain.csproj" />
<ProjectReference Include="..\..\Core\OrderService.Infrastructure\OrderService.Infrastructure.csproj" />
</ItemGroup>
</Project>
I found this already:
Run NUnit tests in .NET Core
None of the provided solutions seem to work for me.
I solved by installing the following NuGet packages
NUnit
NUnit3TestAdapter
Microsoft.NET.Test.Sdk
ReSharper is known to have problems running .NET Core 2.0 tests in Visual Studio 2017. Try to run them without ReSharper.
For more information see this Stack Overflow question and answers: .NET Core 2.0 and xUnit doesn't run.
If you've installed the packages in D.G's answer and it still doesn't work, check if you need to update ReSharper. That worked for me.

Writing a custom task via UsingTask in .NET Core keeps failing to build (missing assemblies)

My goal is to run tasks in .NET Core (akin to rake in Rails land), so I can execute code outside the normal control flow of the application's lifecycle. I have seen there are projects like albacore that supposedly accomplish this, but I'm trying to do it "the .NET way", rather than bringing in a separate ruby dependency to accomplish this.
After following this article on Task Writing for msbuild, I have managed to create a simple Task that implements the ITask interface as suggested in the article:
EcommerceSite/Tasks/ScrapeAll.cs
using System;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace Tasks
{
public class ScrapeAll : Task
{
public override bool Execute()
{
return true;
}
}
}
My .csproj file uses the UsingTask element to register my task code, and I have a Target that invokes the task:
EcommerceSite/EcommerceSite.csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" />
<PackageReference Include="Microsoft.Build.Framework" Version="15.3.409" />
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="15.3.409" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Views\ItemPage\" />
<Folder Include="Tasks\" />
</ItemGroup>
<UsingTask TaskName="Tasks.ScrapeAll" AssemblyFile="bin/Debug/netcoreapp2.0/EcommerceSite.dll" />
<Target Name="ScrapeAll">
<Tasks.ScrapeAll />
</Target>
</Project>
So now, on my command line, I am able to invoke: dotnet msbuild /t:ScrapeAll and yet, I get this error:
error MSB4062: The "Tasks.ScrapeAll" task could not be loaded from the assembly
[redacted]/EcommerceSite/bin/Debug/netcoreapp2.0/EcommerceSite.dll.
Could not load file or assembly 'Microsoft.AspNetCore.Mvc.ViewFeatures,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
The system cannot find the file specified.
[redacted]/EcommerceSite/EcommerceSite.csproj(33,5): error MSB4062:
Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available,
and that the task contains a public class that implements Microsoft.Build.Framework.ITask
So my questions are:
Why is my Task trying to load Microsoft.AspNetCore.Mvc.ViewFeatures, which according to the NuGet docs, contains:
view rendering features such as view engines, views, view components, and HTML helpers
of which my Task does nothing of the sort?
How do I make it try to load this assembly, or if that is not an option, how do I resolve the dependency issue?
I got this working tonight. You can see my solution here.
In essence, I hit 2 problems:
I was including the namespace in the element. Once I remove that and just used the class name, the error went away.
<UsingTask TaskName="PublishValidationTask"
AssemblyFile="C:\git\CustomBuildTargets\PublishPipelineTesting\bin\debug\netcoreapp2.0\PublishPipelineTesting.dll"
/>
Only when I set Importance="High" would text show up on the command line.
C:\git\CustomBuildTargets\PublishPipelineTesting>dotnet msbuild PublishPipelineTesting.csproj
Microsoft (R) Build Engine version 15.7.179.6572 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
PublishPipelineTesting -> C:\git\CustomBuildTargets\PublishPipelineTesting\bin\Debug\netcoreapp2.0\PublishPipelineTesting.dll
This is a test
Hello World
Paths also made things weird. I started with an absolute path, which worked well. From there I was able to figure out the relative path, which was simply starting in the bin directory:
AssemblyFile="bin\debug\netcoreapp2.0\PublishPipelineTesting.dll" />
Once I got that working, and the custom task running, return true/false would allow the build to pass or fail, which is exactly what I wanted. When returning "false", I would now get:
C:\git\CustomBuildTargets\PublishPipelineTesting>dotnet run
PublishPipelineTesting.csproj
The build failed. Please fix the build errors and run again.
When return true out of the task, the build would pass and the project would run:
C:\git\CustomBuildTargets\PublishPipelineTesting>dotnet run
PublishPipelineTesting.csproj
Hello World!
The issue that I was having was that the project that was containing my tasks was targeting the netcoreapp2.1 framework .... I changed it to target netstandard2.0 as well, then went into the target and changed the assembly file path to be the netstandard2.0 instead of netcoreapp2.1. After that, the custom task I have worked perfectly.

How to get .NET Core 2.0 xUnit test reports into VSTS

How do I get .NET Core 2.0 xUnit test reports produced and published into VSTS?
Follow the getting started as per this document:
https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-with-dotnet-test
Importantly, this must be in your test project file:
<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>
Now in your VSTS build configuration you need to pretend like it's all VSTest and not choose or try to use the xUnit runner and reporting formats.
So, add a .NET Core task as v2.0 (preview) and set, in additional to other obvious settings:
Command: test
Arguments: --logger:trx --configuration $(BuildConfiguration)
Now add a good old fashioned Publish Test Results task and set:
Test result format: VSTest
Test results files: **\*.trx
Merge test results: check
Upload test results files: check
I think now the Visual Studio runner will run as xUnit, but produce its own reporting format that VSTS copes with.
Note The only bug I saw was that the 'Run duration' was crazy long in the report.

Resources