How to export test results in xunit(internal template of asp.net core) using cake script? - asp.net-core

I am using VS 2017 professional (version 15.2) with platform of Asp.net core (version 1.1). I am using a testing framework of Xunit (which is in the internal template of asp.net core). I try to use the cake script for running the test cases written in Xunit using the cake script, I need to export test results like passed and failed test case count.
Task("Test").Does(() =>
{
var settings = new DotNetCoreTestSettings
{
Configuration = "Release"
};
var projectFiles = GetFiles("./test/**/*.csproj");
foreach(var file in projectFiles)
{
DotNetCoreTest(file.FullPath, settings);
}
});
When I run this code in cake test execution should be finished but I need detailed test results.
Can anyone please suggest how to export the results of test cases?

This will output test results in MSTest .trx format to a 'TestResults' folder in each project folder:
var settings = new DotNetCoreTestSettings
{
Configuration = "Release",
ArgumentCustomization = args => args.Append("-l trx")
};

Related

Telerik Reporting .NET 5 walkthrough yields error "ConfigurationHelper does not exist in current context"

Following the 'How to Host Reports Service in ASP.NET Core in .NET 5' walk through here and early on they have you paste the following in ConfigureSerivces:
// Configure dependencies for ReportsController.
services.TryAddSingleton<IReportServiceConfiguration>(sp =>
new ReportServiceConfiguration
{
ReportingEngineConfiguration = ConfigurationHelper.ResolveConfiguration(sp.GetService<IWebHostEnvironment>()),
HostAppId = "Net5RestServiceWithCors",
Storage = new FileStorage(),
ReportSourceResolver = new UriReportSourceResolver(
System.IO.Path.Combine(sp.GetService<IWebHostEnvironment>().ContentRootPath, "Reports"))
});
However ConfigurationHelper is flagged as 'does not exist in current context'.
I know I probably need to reference an assembly but I did add all the supposed required dependencies via nuget Telerik.Reporting.Services.AspNetCore.Trial.
So I don't know what assembly I need to get ConfigurationHelper.
I suspect this is a really stupid question because there is virtually nothing on the internet about ConfigurationHelper which means the answer is so simple people don't even need to google it.
So what do I need to add to a brand new ASP.NET Core Web Application 5.0 with nuget Telerik.Reporting.Services.AspNetCore.Trial in order to resolve ConfigurationHelper?
ConfigurationHelper is just a static class in your project, you can rename it if you want, then use it in this line ReportingEngineConfiguration = ConfigurationHelper.ResolveConfiguration(sp.GetService<IWebHostEnvironment>()),
Taken from the article you have posted:
static class ConfigurationHelper
{
public static IConfiguration ResolveConfiguration(IWebHostEnvironment environment)
{
var reportingConfigFileName = System.IO.Path.Combine(environment.ContentRootPath, "appsettings.json");
return new ConfigurationBuilder()
.AddJsonFile(reportingConfigFileName, true)
.Build();
}
}
You can have a look at the demo projects in your installation, the path should be similar to C:\Program Files (x86)\Progress\Telerik Reporting R2 2022\Examples\CSharp.NET 5\ReportingRestServiceCorsDemo

Integration Testing Minimal API .NET 6 - nUnit

With .NET 5 I used the following in my nUnit [SetUp] for each test. This created a host and a client with which I could call my API with full integrations as defined in the Startup:
Microsoft.AspNetCore.TestHost.TestServer _testServer;
HttpClient _testClient;
[SetUp]
public async Task SetUp()
{
// Load up test configuration
IConfiguration config = new ConfigurationBuilder()
.SetBasePath("mypath")
.AddJsonFile("integrationsettings.json")
.Build();
// Create the host (using startup)
WebHostBuilder builder = new WebHostBuilder()
// Use startup from WebApp Server project
.UserStartup<MyWebApp.Startup>()
// Configure logging from integrationsettings.json
.ConfigureLogging((hostingContext, logging) =>
logging.AddConfiguration(config.GetSection("Logging"))
// Set core app configuration to use integrationsettings.json
.ConfigureAppConfiguration(cfg => cfg.AddConfiguration(config))
// Add any additional services not loaded by startup
.ConfigureServices(services => // add additional services not laoded in Startup);
// Create the Server
_testServer = new TestServer(builder);
// Create the Client
_testClient = _testServer.CreateClient();
}
I could then use testClient HttpClient to work directly with my API.
Life was sweet.
I know I could still use the old model with .NET 6 (Program.cs + Startup.cs), but if I'm going to go with the flow, now that we have minimal API with just the Program.cs, how do I replicate the above?
From what I can gather, it looks like WebApplicationFactory is the key, but have not found anything that gets me over the line.
Is it as simple as adding the assembly that contains WebApplication and just build the app in test SetUp in the same way as I do in Program.cs on the server?
Is there a way to encapsulate the build logic (much like the old Startup.cs) so I do not need to duplicate the configuration used on the server in my tests?

Migration to Minimal API - Test Settings Json not overriding Program

Thanks to this answer: Integration test and hosting ASP.NET Core 6.0 without Startup class
I have been able to perform integration tests with API.
WebApplicationFactory<Program>? app = new WebApplicationFactory<Program>()
.WithWebHostBuilder(builder =>
{
builder.ConfigureServices(services =>
{
});
});
HttpClient? client = app.CreateClient();
This has worked using the appsettings.json from the API project. Am now trying to use integrationtestsettings.json instead using:
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(ProjectDirectoryLocator.GetProjectDirectory())
.AddJsonFile("integrationtestsettings.json")
.Build();
WebApplicationFactory<Program>? app = new WebApplicationFactory<Program>()
.WithWebHostBuilder(builder =>
{
builder.ConfigureAppConfiguration(cfg => cfg.AddConfiguration(configuration));
builder.ConfigureServices(services =>
{
});
});
_httpClient = app.CreateClient();
I have inspected the configuration variable and can see the properties loaded from my integrartiontestsettings.json file. However, the host is still running using the appsettings.json from the server project.
Previously, in .Net5, I was using WebHostBuilder and the settings were overridden by test settings.
WebHostBuilder webHostBuilder = new();
webHostBuilder.UseStartup<Startup>();
webHostBuilder.ConfigureAppConfiguration(cfg => cfg.AddConfiguration(_configuration));
But cannot get the test settings to apply using the WebApplicationFactory.
It seems the method has changed.
Changing:
builder.ConfigureAppConfiguration(cfg => cfg.AddConfiguration(configuration));
To:
builder.UseConfiguraton(configuration);
has done the trick.
builder.ConfigureAppConfiguration, now it's configuring the app (after your WebApplicationBuilder.Build() is called) and your WebApplication is created.
You need to "inject" your configurations before the .Build() is done. This is why you need to call UseConfiguraton instead of ConfigureAppConfiguration.

Karate tests run successfully but code coverage shows zero [duplicate]

How to get Jacoco reports for the Karate test feature files using Gradle.
My project is a Gradle project and I am trying to integrate jacoco report feature in my project for the karate tests. The server is running in my local on 8080 port.
I am doing the following way to generate jacoco report and please let me know is my approach correct and also give me a solution to get the jacoco report for the gradle project.
1) First I am trying to generate jacoco execution data with the help of jacocoagent.jar as follows with a Gradle task:
java -javaagent:/pathtojacocojar/jacocoagent.jar=destfile=/pathtojocofile/jacoco.exec -jar my-app.jar
2) Next, I am running a Gradle task to generate the report
project.task ('jacocoAPIReport',type: org.gradle.testing.jacoco.tasks.JacocoReport) {
additionalSourceDirs = files(project.sourceSets.main.allSource.srcDirs)
sourceDirectories = files(project.sourceSets.main.allSource.srcDirs)
classDirectories = files(project.sourceSets.main.output)
executionData = fileTree(dir: project.projectDir, includes: ["**/*.exec", "**/*.ec"])
reports {
html.enabled = true
xml.enabled = true
csv.enabled = false
}
onlyIf = {
true
}
doFirst {
executionData = files(executionData.findAll {
it.exists()
})
}
}
project.task('apiTest', type: Test) {
description = 'Runs the api tests'
group = 'verification'
testClassesDirs = project.sourceSets.apiTest.output.classesDirs
classpath =
project.sourceSets.apiTest.runtimeClasspath
useJUnitPlatform()
outputs.upToDateWhen { false }
finalizedBy jacocoAPIReport
}
I don't see any of my application's classes in the jococo.exec file. I think, bcz of that I am always getting the coverage report as 0%.
The server is running in my local on 8080 port.
I don't think that is going to work. Depending on how your code is structured you need to instrument the code of the server.
I suggest trying to get a simple unit test of a Java method to work with Gradle. If that works, then use the same approach for the server-side code and it will work.

ASP.NET Core 1.0 RC2 on ARM

I used to run an ASP.NET Core 1.0 RC1 project on my Raspberry Pi. Now I've converted the project to RC2 but there don't seem to be any ARM builds available of RC2.
Does anybody know the status of this?
I found the workaround.
1. add the code in startup.cs like below.
services.AddMvc().AddRazorOptions(options =>
{
if (Type.GetType("Mono.Runtime") != null)
{
var myAssemblyPaths = new[] {
"/usr/lib/mono/4.5/mscorlib.dll",
"/usr/lib/mono/4.5/System.Core.dll",
"/usr/lib/mono/4.5/Microsoft.CSharp.dll"
};
var previous = options.CompilationCallback;
options.CompilationCallback = (context) =>
{
if (previous != null)
{
previous(context);
}
var references = myAssemblyPaths.Select(p => MetadataReference.CreateFromFile(p)).ToArray();
context.Compilation = context.Compilation.AddReferences(references);
};
}
});
2. build on windows machine.
3. copy all the web project to linux target including output files.
4. run the app using mono.
My web project name is AspNetCoreMonoTest.
"/root/AspNetCoreMonoTest" folder should have project.json.
Please execute by below command.
cd /root/AspNetCoreMonoTest && mono bin/Debug/net451/win7-x64/AspNetCoreMonoTest.exe
I used mono version 4.4.0. and refered to https://github.com/aspnet/Mvc/issues/4818#issuecomment-224775503