问题描述
我们一直在使用的xUnit框架,我们作为自开始时测试框架的项目。目前,在项目2200+单元测试,一切似乎都很好。
We have been using xUnit Framework in our project as test framework since the begining. Currently there are 2200+ unit tests in project and everything seems fine.
不过昨天我决定运行单元测试在CI构建并每晚构建。与团队生成控制器战斗运行1-2小时,我成功运行测试的xUnit测试。但是有一个问题,有没有像低于22警告abouts测试,
- Xunit.Sdk.EqualException:Assert.Equal()失败
要么
- System.ArgumentNullException:值不能为空
But yesterday i decided to run unit tests at CI builds and nightly builds. Fighting with team build controller to run xunit tests for 1-2 hours i succeed to run tests. But there was a problem, there 22 warning abouts tests like below,- Xunit.Sdk.EqualException: Assert.Equal() Failureor- System.ArgumentNullException: Value cannot be null.
一些研究之后,我意识到这些测试必须失败,但似乎通过,因为所有这些测试都标有异步关键字。
After some more research i realized these tests have to be failed but seems passed because all of these tests are marked with "async" keyword.
其实不应该有任何错误,因为的xUnit支持有关这些帖子异步测试
http://bradwilson.typepad.com/blog/2012/01/xunit19.html
http://sravi-kiran.blogspot.com.tr/2012/11/UnitTestingAsynchronousMethodsUsingMSTestAndXUnit.html
Howerver现实对我来说是细微的差别。是的xUnit运行没有任何问题,但不正确测试。
Actually there shouldn't be any error, because xUnit supports async tests regarding these postshttp://bradwilson.typepad.com/blog/2012/01/xunit19.htmlhttp://sravi-kiran.blogspot.com.tr/2012/11/UnitTestingAsynchronousMethodsUsingMSTestAndXUnit.htmlHowerver reality is slighter difference for me. Yes xUnit runs without any problem but doesn't test properly.
下面是两个小方法。
public async Task<string> AsyncTestMethod() {
throw new Exception("Test");
}
public string NormalTestMethod() {
throw new Exception("Test");
}
正如你所看到唯一的区别,第一种方法定义为异步
这里有两个测试这些方法。
As you can see only difference, the first method defined as "async"And here are two tests for these methods.
[Fact]
public async void XunitTestMethod_Async() {
Program p = new Program();
string result = await p.AsyncTestMethod();
Assert.Equal("Ok", result);
}
[Fact]
public void XunitTestMethod_Normal() {
Program p = new Program();
string result = p.NormalTestMethod();
Assert.Equal("Ok", result);
}
这两个原来的方法抛出异常,所以我觉得这两个测试失败了,但结果是不同的。你可以看到的结果:
Both of the original methods throws exception,so i think both of the tests should fail but the result is different. You can see the results:
XunitTestMethod_Async测试通过,但XunitTestMethod_Normal失败。
抛出异常情况并非如此,你可以改变任何你想要的AsyncTestMethod总是传递方法的内容。
XunitTestMethod_Async test pass but XunitTestMethod_Normal fails.Throwing an exception is not the case, you can change the method content whatever you want AsyncTestMethod always passes.
下面是示例解决方案:https://github.com/bahadirarslan/AsyncTestWithxUnit
我可能误解或想错了,但现在这种行为会导致很多痛苦,我
I might misunderstood or think wrong but now this behaviour causes lots of pain for me
希望你能微启我。
PS:我在GitHub上的xUnit页创建一个问题,但我不能确信这与我或造成的xUnit。所以我决定在这里也问。
问题:
PS: I created an issue at xUnit github page, but i couldn't be sure that is this caused from me or xUnit. So i decided to ask here too.Issue: https://github.com/xunit/xunit/issues/96
推荐答案
的问题是,异步无效
方法不返回任务
对象,该对象的xUnit可能可能检查的异常。的xUnit甚至无法跟踪特定的完成异步无效
方法,而不是一个异步任务
方法。
The problem is that async void
method does not return a Task
object which xUnit could possibly examine for exceptions. xUnit cannot even track the completion of a particular async void
method, as opposed to an async Task
method.
此外,异步无效
方法不相同的堆栈帧抛出异常,甚至从方法的同步部分抛出的异常。相反,异常是通过 SynchronizationContext.Post
传播到当前同步上下文(如果 SynchronizationContext.Current!= NULL
)或以其他方式通过 ThreadPool.QueueUserWorkItem
。此行为是由异步任务
方法,不同。这是可以跟踪的未决异步无效
方法总数(通过 SynchronizationContext.OperationStarted
/ OperationCompleted
),而不是单独的方法。
Moreover, async void
methods do not throw exceptions on the same stack frame, even for exceptions thrown from the synchronous part of the method. Rather, exceptions are propagated to the current synchronization context via SynchronizationContext.Post
(in case SynchronizationContext.Current != null
), or otherwise via ThreadPool.QueueUserWorkItem
. This behavior is different from async Task
methods, more details. It's possible to track the total number of the pending async void
methods (via SynchronizationContext.OperationStarted
/OperationCompleted
), but not individual methods.
所以,从修改
到 XunitTestMethod_Async
方法的签名异步无效异步任务
应该解决的问题。
So, changing the XunitTestMethod_Async
method's signature from async void
to async Task
should solve the problem.
这篇关于异步的xUnit测试工作不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!