Run Xunit tests, that are marked "skip"

Hi community,

I just finished all the learning exercises in the C# track and went on to solve the practice exercises.
All practice exercises that i’ve done so far, have only one test that runs, while all other tests are marked with the “Skip” property.
Is there some command that runs all tests, regardless of the “skip” property, so I don’t have to remove that properties in every exercise? Currently I’m using dotnet test in a PowerShell console inside VS Code.

This is an annoyance to me as well, except on the Rust track. I am tempted to propose to simply un-ignore all tests by default.

As far as I can tell, there isn’t. The reason for the skipped tests is to encourage students to solve exercises using a TDD-style approach, where they implement functionality gradually.

On the Haskell track, by default tests are run until the first failing one. Probably for some other languages this is a possibility too.

For Rust you can use

cargo test – --ignored

One of our former Exercism students I think got a change made in cargo to run both unignored and ignored tests in one command:

cargo test – --include-ignored

Not Cargo, it turns out, but libtest: relevant docs.

@dorianignee It seems to me that @ErikSchierboom is the C# track’s maintainer, so I fear no more satisfying answer is coming.

1 Like

Thank you for your answers!

In my bash track, there was also an option to run all tests, even if they’re marked “skip”. I guess, for the C# track, I just have to comply with the standards and delete the skip-properties when I want to run the tests.

It is not a real solution, but you could write a script to do that for you in a specific folder.

@vaeng That’s a really good idea. Thank you

In the install instructions for the exercism CLI on Linux, there is a script that changes the current working directory to the last downloaded exercise folder. I’ve made something similar for PowerShell and added a function that removes the skip-parameter from a *Tests.cs file and then runs a test. This can be called by “dotnet testall”.

For anyone, who is interested, this is my script file. Just copy the code into the $profile file:

Maybe, that could be added to the install instructions for the exercism CLI on Windows, too.

# catch call to exercism.exe
# this automatically changes the working folder
# when you download a new exercise
function exercism {
    Param(
        [string]$keyword = ""
    )
    
    # run exercism.exe with the given arguments
    exercism.exe $keyword @args
    
    # if keyword is "download", extract exercise and track and go to that folder
    if ($keyword -eq "download") {

        # get exercise
        $exercise = (
            $args | 
            where {$_.StartsWith("--exercise=")} | 
            select -First 1
        ).ToString().Substring(11)

        # get track
        $track = (
            $args | 
            where {$_.StartsWith("--track=")} | 
            select -First 1
        ).ToString().Substring(8)

        # go to that folder
        cd  $env:USERPROFILE\Exercism\$track\$exercise
    }
}

# catch call to dotnet.exe
# adds command "dotnet testall"
# this lets you run all tests, even if they're are marked "skip"
# !!ATTENTION!! : This deletes the "Skip" parameters in the test file
function dotnet {
    Param(
        [string]$keyword = ""
    )

    # if keyword = "testall", remove all skip parameters in test file and run test
    if ($keyword -eq "testall") {
        $testFile = dir -File | where {$_.Name.EndsWith("Tests.cs")} | select -First 1
        $content = Get-Content $testFile -Raw

        # Removement is done with Regex, so the Skip properties will also be removed, 
        # if there's a different text attached to them
        # !!ATTENTION!! : Doesn't work if a closing bracket is in the Skip-string
        $content = [Regex]::Replace($content, '(?i)(?<=\[Fact)\s*\(Skip[^)]*\)', "")

        Set-Content -Path $testFile.FullName -Value $content
        dotnet.exe test @args
    } else {
        # pass all other commands to dotnet.exe directly
        dotnet.exe $keyword @args
    }
}

Are you able to restore the state of the skipped tests afterwards?

I didn’t implement it that way, but since the “dotnet test” command is being run from within the PowerShell script, you can easily restore the original test file like this:

# catch call to dotnet.exe
# adds command "dotnet testall"
# this lets you run all tests, even if they're are marked "skip"
function dotnet {
    Param(
        [string]$keyword = ""
    )

    # if keyword = "testall", remove all skip parameters in test file and run test
    if ($keyword -eq "testall") {
        $testFile = dir -File | where {$_.Name.EndsWith("Tests.cs")} | select -First 1
        $originalContent = Get-Content $testFile -Raw

        # Removement is done with Regex, so the Skip properties will also be removed, 
        # if there's a different text attached to them
        # !!ATTENTION!! : Doesn't work if a closing bracket is in the Skip-string
        $skipFreeContent = [Regex]::Replace($originalContent, '(?i)(?<=\[Fact)\s*\(Skip[^)]*\)', "")

        # run "dotnet test" with the skip free file, then restore the original contents
        Set-Content -Path $testFile.FullName -Value $skipFreeContent
        try {
            dotnet.exe test @args
        } finally {
            Set-Content -Path $testFile.FullName -Value $originalContent
        }
    } else {
        # pass all other commands to dotnet.exe directly
        dotnet.exe $keyword @args
    }
}
1 Like