xUnit test using data coming from external file

xUnit has been designed to be extensible, i.a. via the DataAttribute.

InlineData, ClassData and MemberData all derive from DataAttribute, which you can extend yourself to create a custom data source for a data theory, in which you may read from you external file and use e.g. Json.NET to deserialize your data.

User Sock wrote about this in his blog regarding JSON, as you mentioned:

  • Creating a custom xUnit theory test DataAttribute to load data from JSON files
  • Source on GitHub

Related question with data from CSV file: How to run XUnit test using data from a CSV file

And here are two xUnit samples:

  • ExcelData
  • SqlData

I believe the cleanest way is using ClassData for that so that you can populate data for your test from wherever you like. Consider this:

public class TestData : IEnumerable<object[]> 
{
    private IEnumerable<object[]> ReadFile() 
    {
        //read your file
    }

    public IEnumerator<object[]> GetEnumerator() 
    {
        var items = ReadFile();
        return items.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

Of course, you could just populate data from a file during the Arrange phase of your test and then just loop your test method over the data. But in that case, you would lose the advantage of detecting all failing tests instead of just the first.