xcode 在没有继续运行测试但没有停止其他测试的情况下使用断言使 XCTestCase 失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20998788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
failing a XCTestCase with assert without the test continuing to run but without stopping other tests
提问by Yoav Schwartz
I'm trying to test my application using the XCTest framework.
我正在尝试使用 XCTest 框架测试我的应用程序。
I want my single test case to fail if some logical condition holds (using an assertion). I don't want the rest of the code in the test case to run, because this might lead to problems (access to null pointers, for example) I also want the rest of the test case to run normally, and just the failed test to be marked as failed.
如果某些逻辑条件成立(使用断言),我希望我的单个测试用例失败。我不希望测试用例中的其余代码运行,因为这可能会导致问题(例如,访问空指针)我还希望测试用例的其余部分正常运行,而只是失败的测试被标记为失败。
I've noticed XCTestCase has a property called continueAfterFailure. However, setting it to YES caused the failed test to continue executing lines after the assertion, and setting it to NO caused the rest of the tests not to run at all.
我注意到 XCTestCase 有一个名为 continueAfterFailure 的属性。但是,将其设置为 YES 会导致失败的测试在断言后继续执行行,而将其设置为 NO 会导致其余测试根本无法运行。
Is there a solution to this issue?
这个问题有解决方案吗?
采纳答案by Oscar Hierro
Pascal's answer gave me the idea to achieve this properly. XCTool now behaves like OCUnit when an assertion fails: the execution of the test case is aborted immediately, tearDown invoked and the next test case is run.
帕斯卡的回答给了我正确实现这一目标的想法。当断言失败时,XCTool 现在的行为类似于 OCUnit:立即中止测试用例的执行,调用tearDown 并运行下一个测试用例。
Simply override the method invokeTest
in your base class (the one that inherits from the XCTestCase class):
只需覆盖invokeTest
基类中的方法(从 XCTestCase 类继承的方法):
- (void)invokeTest
{
self.continueAfterFailure = NO;
@try
{
[super invokeTest];
}
@finally
{
self.continueAfterFailure = YES;
}
}
That's it!
就是这样!
回答by nCod3d
The easiest way is to add:
最简单的方法是添加:
continueAfterFailure = false
into setUp() method. So it will look like this:
进入 setUp() 方法。所以它看起来像这样:
Swift
迅速
override func setUp() {
super.setUp()
continueAfterFailure = false
}
Objective-C
目标-C
- (void)setUp {
[super setUp];
[self setContinueAfterFailure:NO];
}
回答by Chris Devereux
One option would be to check the condition normally, then fail and return from the test if it is false.
一种选择是正常检查条件,如果它为假,则失败并从测试返回。
Something like this:
像这样的东西:
if (!condition) {
XCFail(@"o noes");
return;
}
You could wrap this up in a helper macro to preserve readability.
您可以将其包装在一个辅助宏中以保持可读性。
BDD test libraries like Kiwiare more elegant for this sort of thing, as they make it easier to share setup between many tests which leads to fewer assertions per test.
像Kiwi这样的 BDD 测试库在这类事情上更加优雅,因为它们可以更轻松地在许多测试之间共享设置,从而减少每个测试的断言。
回答by Pascal Bourque
I am able to use continueAfterFailure
and let the other tests run by using this pattern:
我能够continueAfterFailure
使用此模式并让其他测试运行:
self.continueAfterFailure = NO;
@try
{
// Perform test code here
}
@finally
{
self.continueAfterFailure = YES;
}
回答by Raphael
In Swift projects, I use a helper function (defined in a shared superclass of all my tests which itself extends XCTestCase
):
在 Swift 项目中,我使用了一个辅助函数(在我所有测试的共享超类中定义,它本身扩展了XCTestCase
):
/// Like `XCTFail(...)` but aborts the test.
func XCTAbortTest(_ message: String,
file: StaticString = #file, line: UInt = #line
) -> Never {
self.continueAfterFailure = false
XCTFail(message, file: file, line: line)
fatalError("never reached")
}
As the comment suggests, the call to fatalError
is never actually executed; XCTFail
aborts the test in an orderly fashion (tearDown
is called, next test runs, etc.). The call is only there to trick the compiler into accepting Never
as return type since XCTFail
returns Void
(it doesreturn if continueAfterFailure == true
).
正如评论所暗示的那样,对 的调用fatalError
从未真正执行过;XCTFail
以有序的方式中止测试(tearDown
被调用,下一次测试运行等)。该调用只是为了诱使编译器接受Never
自XCTFail
返回以来的返回类型Void
(它确实返回 if continueAfterFailure == true
)。
Note that self.continueAfterFailure
is reset to the default true
for every test method. You can also make that explicit in setUp()
.
请注意,每个测试方法self.continueAfterFailure
都重置为默认值true
。您也可以在setUp()
.