Running xUnit tests on Teamcity using async methods

后端 未结 2 1208
天涯浪人
天涯浪人 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:42

    Can anybody explain to me why xUnit running on the teamcity buildserver is not running the test correctly in the first place?

    First, I'd check your xUnit versions - you should be running the recently-released 2.0. I suspect your local version may be out of date.

    The core problem is in this line:

    var resultString = response.Content.ReadAsAsync().Result;
    

    I suspect you're running into a deadlock situation that I describe on my blog. HttpClient has some methods on some platforms that do not properly use ConfigureAwait(false), and is thus subject to this deadlock. xUnit 2.0 installs a single-threaded SynchronizationContext into all its unit tests, which provides the other half of the deadlock scenario.

    The proper solution is to replace Result with await, and to change the return type of your unit test method from void to Task.

提交回复
热议问题