与ASP.Net应用程序集成测试中的步骤自动化

时间:2018-09-03 06:11:23

标签: asp.net .net unit-testing xunit xunit.net

我是dotnet编程中与xUnit系统进行集成测试的新手。我已经用一些测试API控制器的方法编写了一个测试。我只想做一个测试,即在完成特定测试之后,另一种方法应该对先前的测试方法结果做一些工作。但是如何? 我正在使用xunit。

测试规则

[Collection("SystemCollection")]
public class AirportTests
{
    private readonly TestContext _sut;
    private const string AuthToken = "SOME-TOKEN";
    private const string BasePath = "base-endpoint";


    public AirportTests(TestContext sut)
    {
        _sut = sut;
    }

    [Theory]
    [InlineData(AuthToken)]
    public async Task AirportListResponse(string authToken)
    {
        _sut.Client.DefaultRequestHeaders.Add("X-Auth-Token", authToken);

        var res = await _sut.Client.GetAsync($"{BasePath}/List");

        var airportListRs =
            JsonConvert.DeserializeObject<AirportListRs>(await res.Content.ReadAsStringAsync());
        airportListRs.Airports.Should().NotBeNull();
    }

    [Theory]
    [InlineData(AuthToken)]
    public async Task AirportListByIdResponse(string authToken)
    {
        _sut.Client.DefaultRequestHeaders.Add("X-Auth-Token", authToken);

        var req = new AirportListRq {AirportId = new[] {8}};
        var res = await _sut.Client.PostAsync($"{BasePath}/ListById", req.AsJson());

        var airportListRs =
            JsonConvert.DeserializeObject<AirportListRs>(await res.Content.ReadAsStringAsync());
        airportListRs.Airports.Should().NotBeNull();
    }

    [Theory]
    [InlineData(AuthToken)]
    public async Task AirportAddResponse(string authToken)
    {
        _sut.Client.DefaultRequestHeaders.Add("X-Auth-Token", authToken);

        var req = new AirportAddRq
        {
            CityId = 1,
            Code = "testCode",
            Name = "testName"
        };
        var res = await _sut.Client.PostAsync($"{BasePath}/Add", req.AsJson());

        var airportAddRs = JsonConvert.DeserializeObject<AirportAddRs>(await res.Content.ReadAsStringAsync());

        airportAddRs.Success.Should().BeTrue();
    }

    [Theory]
    [InlineData(AuthToken)]
    public async Task AirportEditResponse(string authToken)
    {
        _sut.Client.DefaultRequestHeaders.Add("X-Auth-Token", authToken);
        // SHOULD GET PREVIOUS TEST RESULT WHICH IS AN OBJECT AND DO SOME OTHER TESTS

    }
}

测试上下文

public class TestContext : IDisposable
{
    public HttpClient Client { get; set; }
    private TestServer _server;

    public TestContext()
    {
        SetupClient();
    }

    private void SetupClient()
    {
        var configuration = new ConfigurationBuilder()
            .SetBasePath(AppContext.BaseDirectory)
            .AddJsonFile("appsettings.json")
            .Build();

        _server = new TestServer(new WebHostBuilder().UseConfiguration(configuration).UseStartup<Startup>());

        Client = _server.CreateClient();
    }

    public void Dispose()
    {
        //
    }
}

收藏

[CollectionDefinition("SystemCollection")]
public class Collection : ICollectionFixture<TestContext>
{
}

0 个答案:

没有答案