[英]Trying to make a template test in XCTest
I'm writing automated tests for my company's website and I'm also now writing tests for the iOS app.我正在为我公司的网站编写自动化测试,我现在也在为 iOS 应用程序编写测试。 In my Jasmine tests in javascript I use a .forEach()
loop to create a test template, for example:在我的 javascript 中的 Jasmine 测试中,我使用.forEach()
循环来创建测试模板,例如:
[one, two].forEach(function(number){
it('should print ' + number, function() {
console.log(number);
}
});
// Output:
// 'should print one' #=> one
// 'should print two' #=> two
Is there any way to do this with Swift in XCTest
's?有没有办法用 XCTest 中的XCTest
做到这一点?
Swift has for-in loops: Swift有for-in循环:
let inputs = ["abc", "def", "ghi"]
for input in inputs {
// do something
print(input)
// assert something
XCTAssertTrue(someFunc(input))
}
Read more here: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ControlFlow.html 在此处阅读更多内容: https : //developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ControlFlow.html
You can use XCTContext. runActivity
您可以使用XCTContext. runActivity
XCTContext. runActivity
for this and for each item in your loop create a new activity, so your test method can look like: XCTContext. runActivity
为此和循环中的每个项目创建一个新活动,因此您的测试方法可能如下所示:
func testActivity() throws {
let numbers = ["one", "two", "three"]
for number in numbers {
XCTContext.runActivity(named: "should print \(number)") { activity in
print(number)
}
}
}
This is a much more preferred approach than just running assertions with for loop as in your test report you will be able to visualize which iteration from the loop failed/passed:这是一种比仅使用 for 循环运行断言更可取的方法,因为在您的测试报告中,您将能够可视化循环中的哪个迭代失败/通过:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.