Use the same test data for multiple xUnit Theory runs

I am using xUnit for the unit test of my application and am currently setting up a test to use the attribute [Theory]to check for several different data inputs.

To do this, I need to create test data in my previously considered data context. This works, but when I add data to the test itself, each run ends with the same data addition.

My current test:

[Theory]
[InlineData(null, 2)]
[InlineData("en-AU", 1)]
public void Test1(string term, int expectedCount)
{
    Fixture.DbContext.Culture.Add(new Culture { Name = "English (Australia)", CultureCode = "en-AU", NativeName = "English (Australia)"});
    Fixture.DbContext.Culture.Add(new Culture { Name = "English (United States)", CultureCode = "en-US", NativeName = "English (United States)" });
    Fixture.DbContext.Culture.Add(new Culture { Name = "English", CultureCode = "en", NativeName = "English", NeutralCultureFlag = true });

    var result = controller.GetRegions(term);

    Assert.IsType(typeof (JsonResult), result);
    var jsonResult = (JsonResult)result;

    Assert.Equal(expectedCount, jsonResult.Data);
}

Is there a way to only set up test data once for each run InlineData? I know that I can put it in the constructor of the test class, but I would prefer not to do this, because it seems superfluous if this is the only test in the class that uses this data.

+4
1

InlineData?... , .

, , XUnit Class Fixture ( IClassFixture<>, ), , () , . , .

, , Collection Fixture, , , , .

, / ; , , , .

, , .



, , ? .

+2

Source: https://habr.com/ru/post/1618506/


All Articles