I'm going through a great book learning about test-driven development in Swift. My ultimate goal is to get a better understanding of OOP architecture. As I'm reading the book, a section early on states that the setUp() method is fired before each test method which I understand does the setup of objects to run the test for a pass or fail result. What I'm unsure of is how is this even possible I'm guessing from an architecture stand-point? How was Apple able to make a class that has a method which is fired before every other method in the class?
我正在阅读一本关于Swift中测试驱动开发的好书。我的最终目标是更好地理解OOP架构。在我读这本书时,早期的一节说明在我理解的每个测试方法之前触发了setUp()方法,对象的设置是为了通过或失败结果运行测试。我不确定的是,从架构的角度来看,这是怎么回事? Apple如何能够创建一个具有在类中的每个其他方法之前触发的方法的类?
Here is some sample code:
以下是一些示例代码:
import XCTest
@testable import FirstDemo
class FirstDemoTests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
func testPerformanceExample() {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}
}
3 个解决方案
#1
4
I think XCTest
class has a lifecycle just like UIViewController
for example.
我认为XCTest类的生命周期就像UIViewController一样。
Same as viewDidLoad()
called after init(coder:)
when the view is loaded into memory, setUp()
method is also called after the test class is loaded (once during the life of the object).
与init(coder :)之后调用的viewDidLoad()相同,当视图加载到内存中时,setUp()方法也会在加载测试类后调用(在对象的生命周期内一次)。
You can also investigate native and third party test frameworks source code on github:
您还可以在github上调查本机和第三方测试框架的源代码:
https://github.com/apple/swift-corelibs-xctest
https://github.com/apple/swift-corelibs-xctest
https://github.com/google/googletest
https://github.com/google/googletest
#2
2
You are Subclassing a Class called XCTestCase. Usally Testframework introspects Classes in which Test-Methods are defined and run them in a particular order.
您正在对一个名为XCTestCase的类进行子类化。通常,Testframework会内省定义测试方法的类,并按特定顺序运行它们。
Anyway, im not 100% sure if the XCTest is working this way, but you could try to have a look at the source code and try to dig deeper there:
无论如何,我不是100%确定XCTest是否以这种方式工作,但您可以尝试查看源代码并尝试深入挖掘:
https://github.com/apple/swift-corelibs-xctest
https://github.com/apple/swift-corelibs-xctest
#3
2
For each test to be tested: setup()
is called, then the actual test, then teardown()
.
对于每个要测试的测试:调用setup(),然后调用实际测试,然后调用()。
Then again setup another test from that class and tearDown again...until all** tests of your test class get ran.
然后再次从该类设置另一个测试并再次使用tearDown ...直到测试类的所有**测试都运行完毕。
The sequence of the lines won't affect which test gets called first.
这些行的顺序不会影响首先调用哪个测试。
The reason that it's made this way is because 2 tests should be 100% independent. You don't want testExample do something to the object/sut/system_under_test (mutate its state) you're performing your tests on but, not undo what it's done. Otherwise your testPerformanceExample would be loaded from a state you weren't expecting.
之所以采用这种方式是因为2次测试应该是100%独立的。你不希望testExample对object / sut / system_under_test做一些事情(改变它的状态)你正在执行你的测试,但是不要撤消它做了什么。否则,您的testPerformanceExample将从您不期望的状态加载。
#1
4
I think XCTest
class has a lifecycle just like UIViewController
for example.
我认为XCTest类的生命周期就像UIViewController一样。
Same as viewDidLoad()
called after init(coder:)
when the view is loaded into memory, setUp()
method is also called after the test class is loaded (once during the life of the object).
与init(coder :)之后调用的viewDidLoad()相同,当视图加载到内存中时,setUp()方法也会在加载测试类后调用(在对象的生命周期内一次)。
You can also investigate native and third party test frameworks source code on github:
您还可以在github上调查本机和第三方测试框架的源代码:
https://github.com/apple/swift-corelibs-xctest
https://github.com/apple/swift-corelibs-xctest
https://github.com/google/googletest
https://github.com/google/googletest
#2
2
You are Subclassing a Class called XCTestCase. Usally Testframework introspects Classes in which Test-Methods are defined and run them in a particular order.
您正在对一个名为XCTestCase的类进行子类化。通常,Testframework会内省定义测试方法的类,并按特定顺序运行它们。
Anyway, im not 100% sure if the XCTest is working this way, but you could try to have a look at the source code and try to dig deeper there:
无论如何,我不是100%确定XCTest是否以这种方式工作,但您可以尝试查看源代码并尝试深入挖掘:
https://github.com/apple/swift-corelibs-xctest
https://github.com/apple/swift-corelibs-xctest
#3
2
For each test to be tested: setup()
is called, then the actual test, then teardown()
.
对于每个要测试的测试:调用setup(),然后调用实际测试,然后调用()。
Then again setup another test from that class and tearDown again...until all** tests of your test class get ran.
然后再次从该类设置另一个测试并再次使用tearDown ...直到测试类的所有**测试都运行完毕。
The sequence of the lines won't affect which test gets called first.
这些行的顺序不会影响首先调用哪个测试。
The reason that it's made this way is because 2 tests should be 100% independent. You don't want testExample do something to the object/sut/system_under_test (mutate its state) you're performing your tests on but, not undo what it's done. Otherwise your testPerformanceExample would be loaded from a state you weren't expecting.
之所以采用这种方式是因为2次测试应该是100%独立的。你不希望testExample对object / sut / system_under_test做一些事情(改变它的状态)你正在执行你的测试,但是不要撤消它做了什么。否则,您的testPerformanceExample将从您不期望的状态加载。