Code Coverage int .NET 5 Web API with Coverlet - code-coverage

I've web Api build it .NET 5.0.
Web API structure:
Main Web Project (Contains controllers and other client specific data)
Infrastructure Project (It has services and repositories with more business logic)
Models (Db entities)
Tests (Test cases written in Xunit)
I have also added XUnit.Coverlet.Collector and XUnit.Coverlet.MSBuild projects too.
The issue:
I'm not sure how to guide coverlet to use existing tests which are written in Tests project to the controller of main app.
For now I have just created one controller in web api and their
tests in both collector and msbuild project. this is working fine, I
can generate code coverage for this controller i.e.
PrimeController.cs (and then there are other classes and controllers with no coverage in reports.)
If I follow this working process, I've to copy all tests twice in both projects.
Isn't there any way I can just give a reference of my Tests project to msBuild and Collector?
)
Any help would be really appreciated.

Related

How to debug Web API through a separate Web project on same machine?

I'm taking over a project from someone who is no longer avaiable to ask how/why their prjoect works.
I have two solutions, one a MVC project and the other a Web API. The MVC project calls the separate API solution to do some validation of data. If I start up the Web API project it runs on localhost:64633. Then I need to start up the MVC project so that I can step through the code and all the way through the Web API.
Thing is that the MVC project runs on localhost:64633 as well so once I have the API running and then try to debug the MVC project I get an error "Unable to start program localhost:64633. An operation is not legal in the current state.".
How can I debug both the MVC project and Web API project at the same time?
Run Your project using local IIS server, follow below steps for that
Right click on project file choose property
Go to Web tab, under the Servers header choose Local IIS
Then click "Create virtual Directory" button
Do above steps to both project. please try now
Note : Run both project with in single solution, automatically debug both project at a time.(Just suggestion only)

TFS Branch Merging on two repositories

We have three different projects using the same sql server database. So the applications are designed in such a way that there are three web projects and one class library project (core i.e the ORM entity framework and business logic). Each of the web project solution consumes this core project. On TFS, we have four different repository for each one of this.
Now we need to enhance one of the web project and the corresponding business layer changes in core class library. As all of these applications are in production, we decided to branch the code to develop this enhancement while supporting the current production issues.
So we branched the webproject repository to have Main branch for production support issues and Dev branch for enhancements. We followed the same branching convention in Core class library project. The VS solution in each branch is referencing the core project / dll at differnt locations. The merge is creating unnecessary conflicts.
Could anyone please advise what is the best way to achieve branching in this scenario?
Since the web project reference to the core project, core project should be updated before web project. Once the Dev branch of core project done, you can merge it to the Main branch. So you don't need to reference Dev branch core project, you just need to make both Main and Dev branches of the web project reference to the Main branch of core project.
Actually, we suggest you build a nuget package for the core project and publish it to nuget.org. Then in web project you would use Manage Nuget packages to install the core dlls and it will install the version you choose of the core dlls.

Code coverage in system tests

We've got automated coverage builds, but they only give us numbers for our unit tests. We've also got a bunch of system tests.
This leaves us with two problems: some code looks uncovered even though it's used in the system tests (WCF endpoints, DB access, etc.); and some code looks covered even though it's only used by the unit tests.
How do I set up NCover (running on a build server) to get coverage numbers from that process (a service) while running these unit tests? All of the processes are on the same box.
In fact, we have two services talking to each other, and both communicate with an ASP.NET MVC app and an IIS-hosted WCF service; so it's actually multiple processes.
(.NET 4.0, x64. Using NUnit and MSpec. CI server is TeamCity.)
Just to clarify, are over there and over here on the same build server?
If so, I assume the basic issue is how to cover multiple services (sorry If I've oversimplified).
If that's true, unfortunately, NCover 3 can't profile more than one service at a time. However, you can cover each service individually (sequentially, not simultaneously) and then merge the coverage files.
I realize this means running NCover a couple of times in your build script, but from a coverage perspective, that's how it would work.
Does this help?

How do I coordinate settings between my ASP.NET MVC 3 app and SpecFlow?

So I have a VS 2010 solution containing one ASP.NET MVC 3 project and one SpecFlow project (with NUnit as test runner) to test the former. When testing under SpecFlow (by running the SpecFlow project), I'd like to change a parameter in the MVC application's ConfigurationManager.AppSettings dictionary, to make it connect to a test database. This change does not seem to affect the running app however, so I guess the test and the app do not share state.
How can I share this application setting between the MVC app project and the SpecFlow project, so they use the same database?
Edit:
I found that the problem wasn't really one of sharing settings between projects (MVC and SpecFlow), but rather between two processes. The reason is that I test MVC via WatiN, which means that the development server, running in a process of its own, is being exercised by the tests. As such the premise of my original question was wrong from the onset, and I consider Jason's answer valid given the original premise.
In your test project (SpecFlow) create a new app.config (unless it's already there) and add the appSettings section to that, with your test database connection string. IIRC the website will hopefully pick up the connection string from the appSettings of the test project, rather then the website.

TFS and code coverage for web application (MVC) assemblies not working

I've got an MVC web application with associated controller tests that run under a TFS build as per normal.
I can see the tests running and passing in the build log and they appear in the "Result details for Any CPU/Release" section of the build
I also have a number of other assemblies with associated tests that are running in the same build. Tests are passing and the details are being shown in the results and logs just fine.
I've enabled code coverage in the build script and the testrunconfig.
The coverage is appearing for all assemblies EXCEPT the web application even though it looks like the tests have been run for it.
Is there anything obvious that I have missed or some sort of work around that I need to do?
I've searched around for a while and haven't found an answer.
Has anyone got code coverage working for MVC web applications using TFS?
After reading the article posted earlier I thought I would try an alternate approach.
looking at the localrun.testconfig in a text editor I saw that the assemblies that contained code coverage data were in the section below
<Regular>
<CodeCoverageItem ... />
<CodeCoverageItem ... />
</Regular>
The only reference to a web project was in the section:
<AspNet>
<AspNetCodeCoverageItem id="..." name="..." applicationRoot="/" url="http://localhost:0/" />
</AspNet>
So to get code coverage working in TFS:
In VS IDE, double click the localrun.testconfig, then click Code Coverage and then manually add the MVC assembly by clicking the Add Assembly... button. You will get a warning message about duplicate assemblies, tell it to use this new file.
What this does behind the scenes in the localrun.testconfig is add a CodeCoverageItem
entry with the MVC assembly and removes the AspNet section from the CodeCoverage section of the file.
This has the added benefit of not starting up a local web server every time you run your tests.
But it may have a detrimental effect on any Watin type tests and coverage.
Team Build, Code Coverage and MVC by Richard Fennell looks a pretty detailed hack.

Resources