Unit Test Code Coverage through command line - code-coverage

I am trying to get CodeCoverage of my Unit Test for .Net assemblies. Though I am getting .Coverage files but It does not have any content in it. (Empty).
I am using following command through command line:
'"vstest.console.exe" "C:\MyCode\bin\TestABC.dll" "C:\MyCode\bin\TestClassAdi.dll" /TestCaseFilter:"FullyQualifiedName~TestABC.Func1" /EnableCodeCoverage /Logger:"trx" '
I have tried to run above command using runsettings file as well.
'''
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<!-- Configurations for data collectors -->
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<Configuration>
<CodeCoverage>
<SymbolSearchPaths>
<Path>C:\MyCode\bin\</Path>
</SymbolSearchPaths>
<ModulePaths>
<Exclude>
<ModulePath>.*CPPUnitTestFramework.*</ModulePath>
</Exclude>
<Include>
<ModulePath>C:\MyCode\bin\</ModulePath>
<ModulePath>.*\.exe$</ModulePath>
</Include>
</ModulePaths>
<!-- We recommend you do not change the following values: -->
<UseVerifiableInstrumentation>True</UseVerifiableInstrumentation>
<AllowLowIntegrityProcesses>True</AllowLowIntegrityProcesses>
<CollectFromChildProcesses>True</CollectFromChildProcesses>
<CollectAspDotNet>False</CollectAspDotNet>
</CodeCoverage>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
<MSTest>
<MapInconclusiveToFailed>True</MapInconclusiveToFailed>
<CaptureTraceOutput>false</CaptureTraceOutput>
<DeleteDeploymentDirectoryAfterTestRunIsComplete>False</DeleteDeploymentDirectoryAfterTestRunIsComplete>
<DeploymentEnabled>False</DeploymentEnabled>
<AssemblyResolution>
<Directory path="C:\MyCode\bin\" includeSubDirectories="false"/>
</AssemblyResolution>
</MSTest>
</RunSettings>
'''

Related

Coverlet doesn't appear to respect Function Exclusions in my .runsettings file

I have a .runsettings file which excludes autogenerated methods that are typically associated with async/await code, i.e. MoveNext() in my case. This is my .runsettings file:
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<Configuration>
<CodeCoverage>
<Functions>
<Exclude>
<Function>.*MoveNext.*</Function>
</Exclude>
</Functions>
<UseVerifiableInstrumentation>True</UseVerifiableInstrumentation>
<AllowLowIntegrityProcesses>True</AllowLowIntegrityProcesses>
<CollectFromChildProcesses>True</CollectFromChildProcesses>
<CollectAspDotNet>False</CollectAspDotNet>
</CodeCoverage>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>
This is the command I use to run my tests:
dotnet test --collect:"XPlat Code Coverage" --results-directory .coverage --settings tests.runsettings
If I enable logging I can see that it definitely finds the .runsettings file. However, all the generated coverage files include MoveNext() methods, e.g.
<class name="MyProject.Domain.Services.BackgroundService/<CreateMessages>d__10" filename="MyProject.Domain\Services\BackgroundService.cs" line-rate="0" branch-rate="0" complexity="2">
<methods>
<method name="MoveNext" signature="()" line-rate="0" branch-rate="0" complexity="2">
<lines>
<line number="71" hits="0" branch="False" />
<line number="72" hits="0" branch="False" />
<line number="73" hits="0" branch="True" condition-coverage="0% (0/2)">
<conditions>
<condition number="318" type="jump" coverage="0%" />
</conditions>
</line>
</lines>
</method>
</methods>
After the coverage reports are generated, I merge them using:
dotnet-coverage merge .coverage\*.xml --remove-input-files --recursive -f xml -o coverage.xml
The problem is that these methods are included in the coverage file, and our SonarQube scan job will occasionally fail with the error
[07:43:57]W: [Step 1/1] ERROR: Error during SonarQube Scanner execution
[07:43:57]W: [Step 1/1] ERROR: Line 132 is out of range in the file [...]
We traced this problem to one of those the auto-generated MoveNext() methods. Interestingly enough, when I merge the files using ReportGenerator it doesn't include these methods:
reportgenerator -reports:.coverage/**/*.cobertura.xml -reporttypes:Cobertura -targetdir:.
I can then "merge" the files to get into the correct format using:
dotnet-coverage merge .\Cobertura.xml -f xml -o coverage.xml
This gives me a coverage file with the MoveNext() methods excluded, and SonarQube is happy. This is a needless complexity though, and I would prefer just to have them excluded by Coverlet.
Is there any way to achieve this?

wsImport: jaxb nameXmlTransform not working: A class/interface with the same name is already in use

I need to generate Java classes from a WSDL file.
I'm using the jaxws-maven-plugin plugin configured in this way:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>wsimportPhase</id>
<goals>
<goal>wsimport</goal>
</goals>
<configuration>
<keep>true</keep>
<verbose>true</verbose>
<packageName>my.package.name</packageName>
<wsdlFiles>
<wsdlFile>${basedir}/wsdl/myWSDL.wsdl</wsdlFile>
</wsdlFiles>
<wsdlLocation>wsdl/myWSDL.wsdl</wsdlLocation>
<bindingDirectory>wsdl</bindingDirectory>
<bindingFiles>
<bindingFile>myBinding.xml</bindingFile>
</bindingFiles>
<sourceDestDir>${basedir}/src/main/java</sourceDestDir>
</configuration>
</execution>
</executions>
</plugin>
And I have configured (with some difficulties) the JAXB bindings in this way:
<?xml version="1.0" encoding="UTF-8"?>
<jaxws:bindings version="2.1"
schemaLocation="myWSDL.wsdl"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
<jaxws:bindings node="wsdl:definitions/wsdl:types/xs:schema/xs:import">
<jaxb:bindings schemaLocation="importedXSD_1.xsd">
<jaxb:bindings node="xs:complexType[#name='DuplicatedType']">
<jaxb:schemaBindings>
<jaxb:nameXmlTransform>
<jaxb:typeName suffix="Suffix1"/>
<jaxb:elementName suffix="Suffix1"/>
</jaxb:nameXmlTransform>
</jaxb:schemaBindings>
</jaxb:bindings>
</jaxb:bindings>
<jaxb:bindings schemaLocation="importedXSD_2.xsd">
<jaxb:bindings node="xs:complexType[#name='DuplicatedType']">
<jaxb:schemaBindings>
<jaxb:nameXmlTransform>
<jaxb:typeName suffix="Suffix2"/>
<jaxb:elementName suffix="Suffix2"/>
</jaxb:nameXmlTransform>
</jaxb:schemaBindings>
</jaxb:bindings>
</jaxb:bindings>
</jaxws:bindings>
</jaxws:bindings>
But when I run mvn install, I have the following error:
[INFO] jaxws:wsimport args: [-keep, -s, '/workspace/myproject/src/main/java', -d, '/workspace/myproject/target/classes', -verbose, -encoding, UTF-8, -Xnocompile, -p, my.package.name, -wsdllocation, wsdl/myWSDL.wsdl, -b, 'file:/workspace/myproject/src/main/resources/wsdl/mybinding.xml', "file:/workspace/myproject/src/main/resources/wsdl/myWSDL.wsdl"]
parsing WSDL...
[ERROR] A class/interface with the same name "my.package.name.DuplicatedType" is already in use. Use a class customization to resolve this conflict.
The XPath finds correctly the needed complexType in the xsd, so I don't understand how to fix it.
I have found the solution combining the solutions explained in these two questions:
wsimport - how to generate service endpoint classes and JAXB classes in separate projects/folders
JAXB schema to Java Different XmlRootElement name and Class name
I have made the following steps:
I have created two binding files, one for jaxws-binding and the other one for jaxb-binding.
These are the two files:
jaxws-binding.xml:
<?xml version="1.0" encoding="UTF-8"?>
<jaxws:bindings version="2.1"
schemaLocation="myWSDL.wsdl"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
<jaxws:bindings node="wsdl:definitions/wsdl:types/xs:schema/xs:import">
</jaxws:bindings>
</jaxws:bindings>
jaxb-binding.xml:
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings version="1.0"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<jaxb:bindings schemaLocation="importedXSD_1.xsd" node="xsd:complexType[#name='DuplicatedType']">
<jaxb:class name="DuplicatedTypeSuffix1"/>
</jaxb:bindings>
<jaxb:bindings schemaLocation="importedXSD_2.xsd" node="xsd:complexType[#name='DuplicatedType']">
<jaxb:class name="DuplicatedTypeSuffix2"/>
</jaxb:bindings>
</jaxb:bindings>
I have declared the two binding files in the <bindingFiles> tag of the jaxb-maven-plugin in the pom.xml
I was doing integration with some company and they gave me their WSDL which I had to import into my project.
Running the below would fail due to naming conflict
wsimport -keep -verbose http://ip:port/path/service?wsdl
I resolved the issue by adding "-XautoNameResolution"
wsimport -keep -verbose -XautoNameResolution http://ip:port/path/service?wsdl
And that was it to resolve my pain point.
Reference (04-08-2021)
https://briskwalk.wordpress.com/2012/03/30/a-classinterface-with-the-same-name-is-already-in-use-error-when-generating-stubs-for-net-service-using-jdk-6-wsimport-command/

Specflow External steps assembly - No matching step definition found for one or more steps

Using VS 2015, Specflow 2.2 and MSTest.
I have a test project containing a feature file, the steps are contained in a library project (that compiles to LCC.Integration.Common.Components.dll) of the same solution. There's a project reference from the test project to the library project.
The app.config file of the test project contains the following:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="specFlow" type="TechTalk.SpecFlow.Configuration.ConfigurationSectionHandler, TechTalk.SpecFlow" />
</configSections>
<specFlow>
<!-- For additional details on SpecFlow configuration options see http://go.specflow.org/doc-config -->
<stepAssemblies>
<stepAssembly assembly="LCC.Integration.Common.Components" />
</stepAssemblies>
<unitTestProvider name="MsTest" />
</specFlow>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
</startup>
</configuration>
At design time, the binding from the feature to the steps seems to work fine:
I can right-click any step within the feature, select "Go to Step Definition" and the navigation to the correct step within the library project works fine.
However, when I run the test, the test explorer shows the test as "Skipped" with the following message: "Assert.Inconclusive failed. No matching step definition found for one or more steps"

F# NLog config file

I'm trying to use NLog in an F# console application, I've managed to get it working using a configuration section in App.config however I can't get it working using a stand-alone NLog.config file. My NLog.config file is in the app route, just under App.config and the contents are:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true">
<targets>
<target name="stdFile" xsi:type="File" fileName="c:/temp/compliant.log"/>
<target name="display" xsi:type="OutputDebugString"/>
</targets>
<rules>
<logger name="compliant.mail.*" minlevel="Debug" writeTo="stdFile,display" />
</rules>
</nlog>
What am I doing wrong?
Also, intellisense isn't working for the xml even though I have included the xsd. :(
In your project, in the Properties for NLog.config, do you have NLog.config marked as "Copy Always"?

ASP.NET MVC 2 + Common.Logging + NLog = Session_Start called for each request

After switching the logging library behind Common.Logging 2.1.1 from log4net to NLog 2.0 my ASP.NET MVC 2 application kept logging correctly, but it started calling the HttpApplication.Session_Start method for each request.
I'm trying to use NLog's File target with the following configuration files:
web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="common">
<section
name="logging"
type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>
.
.
.
<configSections>
.
.
.
<common>
<logging>
<factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog20">
<arg key="configType" value="FILE" />
<arg key="configFile" value="~/NLog.config" />
</factoryAdapter>
</logging>
</common>
</configuration>
NLog.config
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!--
See http://nlog-project.org/wiki/Configuration_file
for information on customizing logging rules and outputs.
-->
<targets async="true">
<target
name="f"
xsi:type="File"
fileName="${basedir}/bin/statistics/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${callsite} ${message}"/>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="f" />
</rules>
</nlog>
I have already tried the following:
Debugging the application. The ASP.NET_SessionId cookie is being sent to the server and the Session.SessionID property is not changing between requests.
Switching back to Common.Logging - log4net to verify that the problem is related to Common.Logging - NLog. It works.
Omitting the async="true" attribute in the targets node of the configuration file of NLog to disable the AsyncWrapper of NLog. It doesn't work.
Using other NLog targets, tried Debugger and Database. It works.
I need to hold to the File target and I'd like to use NLog.

Resources