How to use XUnit Schema?

44 Views Asked by At

Please can someone assist me in how to integrate the XUnit schema to avail of the stopOnFail functionality.

I would like to stop the execution my unit tests if one fails.

By looking at:https://xunit.net/docs/configuration-files

Please correct me where I am wrong but I have:

  1. Added a new file to the root of my test project: xunit.runner.json
  2. Updated the .csproj file of my test project to include:
<ItemGroup>
   <Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
  1. I have added the entire contents of: https://xunit.net/schema/current/xunit.runner.schema.json to my xunit.runner.json and updated the stopOnFail to the following:
"stopOnFail": {
    "description": "Enable or disable stopping running further tests once a failed test has been recorded.",
    "default": true,
    "type": "boolean"
}

This is not working, can someone advise me where I am going wrong please?

2

There are 2 best solutions below

7
Etienne de Martel On BEST ANSWER

You misread the documentation, so let's read it again.

Step 1 states:

Add a new JSON file to the root of your test project. Name the file xunit.runner.json. Start with a schema reference so that text editors (like Visual Studio & Visual Studio Code) can provide auto-complete behavior while editing the file

That means starting with a xunit.runner.json file that contains the url to the schema file, not the schema file's contents:

{
  "$schema": "https://xunit.net/schema/current/xunit.runner.schema.json"
}

The JSON schema for the configuration file isn't a valid configuration file in itself, don't use it like that. The $schema property is just there so editors know where to look for the schema so they can help you with validation.

Then, if you keep reading, you'll see that the stopOnFail property is a boolean with a default value of false. So, to enable it, you just need to set it to true. The result is the following configuration file:

{
  "$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
  "stopOnFail": true
}

And then, you have to make sure your runner supports the feature, as it was added in version 2.5.0.

1
Ricudo On

The xunit.runner.json file included next to lines must be enough. (more information here):

{
  "$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
  "stopOnFail": true
}