.NET test project with a single command

Sometimes, I need to create quickly a new .NET test project because I’m learning or practicing and I want, in a few seconds, a new and clean project to work with it.

It’s true that you can use Visual Studio and with only a few clicks, achieve the same result. But, why not automatize these steps and gain some valuable seconds?

It’s obvious that the difference between the manual process and the automatized process it’s not going to be a key advantage, but I have to be honest. I have created this snippet because I can. I wanted to do it because I had fun doing it :)

The necessary steps to create the project are the following (assuming you want to create a FizzBuzz test project):

mkdir FizzBuzz
cd .\FizzBuzz\
echo "# FizzBuzz" > README.md
dotnet new sln
mkdir src
dotnet new classlib -n FizzBuzz -o .\src\
dotnet sln add .\src\FizzBuzz.csproj
mkdir tests
dotnet new xunit -n FizzBuzz.Tests -o .\tests\
dotnet sln add .\tests\FizzBuzz.Tests.csproj
dotnet add .\tests\FizzBuzz.Tests.csproj reference .\src\FizzBuzz.csproj
dotnet add .\tests\FizzBuzz.Tests.csproj package FluentAssertions
dotnet add .\tests\FizzBuzz.Tests.csproj package NSubstitute
dotnet add .\tests\FizzBuzz.Tests.csproj package NSubstitute.Analyzers.CSharp
dotnet new tool-manifest
dotnet tool install dotnet-reportgenerator-globaltool
dotnet add .\tests\FizzBuzz.Tests.csproj package coverlet.msbuild

With these two lines, you can generate a friendly coverage report in HTML.

dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura
dotnet reportgenerator -reports:"tests\coverage.cobertura.xml" -targetdir:"tests\coveragereport" -reporttypes:Html

So far, we have a recipe, but with a little extra effort, we can have a truly single command to generate our test project. For this, you have to edit your profile file with code $PROFILE and paste this function into it (where we can also add the creation of the repository and the .gitignore file):

function New-TestProject {
    param (
        [Parameter(Mandatory=$True)]
        [string]
        $name
    )

    if (Test-Path -Path $name) {
        Write-Error "Project $name already exists"
        return
    }       
     
    mkdir $name
    cd .\$name\
    echo "# $name" > README.md
    dotnet new sln
    mkdir src
    dotnet new classlib -n $name -o .\src\
    dotnet sln add ".\src\$name.csproj"
    mkdir tests
    dotnet new xunit -n "$name.Tests" -o .\tests\
    dotnet sln add ".\tests\$name.Tests.csproj"
    dotnet add ".\tests\$name.Tests.csproj" reference ".\src\$name.csproj"
    dotnet add ".\tests\$name.Tests.csproj" package FluentAssertions
    dotnet add ".\tests\$name.Tests.csproj" package NSubstitute
    dotnet add ".\tests\$name.Tests.csproj" package NSubstitute.Analyzers.CSharp
    dotnet new tool-manifest
    dotnet tool install dotnet-reportgenerator-globaltool
    dotnet add ".\tests\$name.Tests.csproj" package coverlet.msbuild
    New-Item coverage.ps1
    Add-Content coverage.ps1 "dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura"
    Add-Content coverage.ps1 'dotnet reportgenerator -reports:"tests\coverage.cobertura.xml" -targetdir:"tests\coveragereport" -reporttypes:Html'
    Invoke-RestMethod -Method Get -Uri "https://www.toptal.com/developers/gitignore/api/visualstudio,visualstudiocode,csharp" | Out-File .gitignore
    Add-Content .gitignore "tests/coveragereport"
    git init
    git add .
    git commit -m "Initial commit"      
}

Now, to create a test project, a bare command like this New-TestProject FizzBuzz will do the magic. Also, for getting code coverage you can execute .\coverage.ps1. Fast and cheap!

Bye!


Ver también