为什么在@interface 中声明的变量中的值在 XCTest 的方法之间不存在?
Why don't the values in variables declared in @interface persist between methods in XCTest?
我正在 Xcode 中为 运行 一些 UI 测试编写脚本,我想使用一些全局变量。
我的第一次尝试是在 @interface
中声明一个类型为 strong
的变量,如下所示:
@interface Extended_Tests : XCTestCase
@property (strong) NSMutableArray *list;
@property (strong) NSString *name;
@end
但这没有用。
我最终使用老式的 C 方法在方法外声明变量。
我的问题是,为什么这行不通?为什么变量中的值不能在所有方法中保持不变?
编辑:
我的方法:
- (void)testMultiUser1 {
[[[XCUIApplication alloc] init] launch];
XCUIApplication *app = [[XCUIApplication alloc] init];
[app.buttons[@"Sign-in button"] tap];
sleep(5);
user1 = [app.staticTexts elementBoundByIndex:0].label;
[app.otherElements[@"LibraryView"] tap];
sleep(5);
_list = [[NSMutableArray alloc] init];
for(int i = 0; i < 3; i++){
XCUIElementQuery *file1 = [[app.cells elementBoundByIndex:i] descendantsMatchingType:XCUIElementTypeStaticText];
NSString *number = [file1 elementBoundByIndex:0].label;
[_list addObject:number];
}
XCTAssert(_list);
}
我希望这会将变量保存到数组 _list 中,这样我就可以在另一个方法中使用它,如下所示:
-(void)testMultiUser3{
//Go into Library and make sure top 3 files are different from user1
XCUIApplication *app = [[XCUIApplication alloc] init];
[app.otherElements[@"LibraryView"] tap];
sleep(5);
NSMutableArray *user2files = [[NSMutableArray alloc] init];
for(int i = 0; i < 3; i++){
XCUIElementQuery *list1 = [[app.cells elementBoundByIndex:i] descendantsMatchingType:XCUIElementTypeStaticText];
NSString *number = [list1 elementBoundByIndex:0].label;
[user2files addObject:number];
}
XCTAssert(!([user2files[0] isEqualToString:_list[0]] && [user2files[1] isEqualToString:_list[1]] && [user2files[2] isEqualToString:_list[2]]));
}
您的问题是 XCTest 特有的。
每个 运行 的测试在您的测试用例 class 的新实例中是 运行。在您的例子中,每个测试都会实例化一个新的 Extended_Tests
对象。这就是为什么如果您在一个测试中设置任何一个变量,您将不会在另一个测试中看到这些变化。
一般来说,测试最好不要依赖于其他测试的副作用,因为您将无法 运行 自己进行这些测试。最好创建一个共享设置方法来设置状态并从两个状态使用。
如果您绝对必须在测试之间共享变量,那么您可以对静态变量使用 class 静态方法(使用 + 而不是 - 声明的方法)或像您一样使用全局变量。
我正在 Xcode 中为 运行 一些 UI 测试编写脚本,我想使用一些全局变量。
我的第一次尝试是在 @interface
中声明一个类型为 strong
的变量,如下所示:
@interface Extended_Tests : XCTestCase
@property (strong) NSMutableArray *list;
@property (strong) NSString *name;
@end
但这没有用。
我最终使用老式的 C 方法在方法外声明变量。
我的问题是,为什么这行不通?为什么变量中的值不能在所有方法中保持不变?
编辑: 我的方法:
- (void)testMultiUser1 {
[[[XCUIApplication alloc] init] launch];
XCUIApplication *app = [[XCUIApplication alloc] init];
[app.buttons[@"Sign-in button"] tap];
sleep(5);
user1 = [app.staticTexts elementBoundByIndex:0].label;
[app.otherElements[@"LibraryView"] tap];
sleep(5);
_list = [[NSMutableArray alloc] init];
for(int i = 0; i < 3; i++){
XCUIElementQuery *file1 = [[app.cells elementBoundByIndex:i] descendantsMatchingType:XCUIElementTypeStaticText];
NSString *number = [file1 elementBoundByIndex:0].label;
[_list addObject:number];
}
XCTAssert(_list);
}
我希望这会将变量保存到数组 _list 中,这样我就可以在另一个方法中使用它,如下所示:
-(void)testMultiUser3{
//Go into Library and make sure top 3 files are different from user1
XCUIApplication *app = [[XCUIApplication alloc] init];
[app.otherElements[@"LibraryView"] tap];
sleep(5);
NSMutableArray *user2files = [[NSMutableArray alloc] init];
for(int i = 0; i < 3; i++){
XCUIElementQuery *list1 = [[app.cells elementBoundByIndex:i] descendantsMatchingType:XCUIElementTypeStaticText];
NSString *number = [list1 elementBoundByIndex:0].label;
[user2files addObject:number];
}
XCTAssert(!([user2files[0] isEqualToString:_list[0]] && [user2files[1] isEqualToString:_list[1]] && [user2files[2] isEqualToString:_list[2]]));
}
您的问题是 XCTest 特有的。
每个 运行 的测试在您的测试用例 class 的新实例中是 运行。在您的例子中,每个测试都会实例化一个新的 Extended_Tests
对象。这就是为什么如果您在一个测试中设置任何一个变量,您将不会在另一个测试中看到这些变化。
一般来说,测试最好不要依赖于其他测试的副作用,因为您将无法 运行 自己进行这些测试。最好创建一个共享设置方法来设置状态并从两个状态使用。
如果您绝对必须在测试之间共享变量,那么您可以对静态变量使用 class 静态方法(使用 + 而不是 - 声明的方法)或像您一样使用全局变量。