Running xUnit tests on Teamcity using async methods

后端 未结 2 1205
天涯浪人
天涯浪人 2021-01-14 06:59

I made the following xUnit test which is using a HttpClient to call a status api method on a webserver.

[Fact]
public void AmIAliveTest()
{
    var server =         


        
2条回答
  •  温柔的废话
    2021-01-14 07:53

    Your Tests are broken.

    xUnit needs the Task return to be able to figure out that a test failed and more to the point, it is the handle where the exception bubbles back up to xUnit.

    By having a public async void, you have an orphaned Task that exceptioned. The resultant exception of course isn't handled, and of course therefore blows up the entire Process. Therefore your test run stops.

    You can fix it by writing all your tests like this...

    [Fact]
    public async Task AmIAliveTest()
    {
       var server = TestServer.Create();
    
       var httpClient = server.HttpClient;
       var response = await httpClient.GetAsync("/api/status");
    
       response.StatusCode.Should().Be(HttpStatusCode.OK);
       var resultString = await response.Content.ReadAsAsync();
    
       resultString.Should().Be("I am alive!");
    }
    

提交回复
热议问题