gpt4 book ai didi

iphone - 如何编写 OCUnit 测试用例

转载 作者:行者123 更新时间:2023-11-28 21:13:09 26 4
gpt4 key购买 nike

我想使用 Apple 的默认 SenTestingKit 为以下方法编写单元测试:

- (NSDictionary*)getValueTags {
return _tags;
}

- (NSString*)getFlag {
NSString* jo = @"";
for (NSString* key in _tags) {
jo = [jo stringByAppendingFormat:@"%@=\"%@\"&", key, [_tags objectForKey:key]];
}
if ([jo length] > 0) {
jo = [jo substringToIndex:[jo length] - 1];
}
return jo;
}

我使用默认的 SenTesting

    - (void)setUp
{
[super setUp];

// Set-up code here.
}

- (void)tearDown
{
// Tear-down code here.

[super tearDown];
}

-(void)testValueTags{

}

-(void)testGetFlag{

}

我是编写测试用例的新手,我需要一些示例方法指南来编写测试用例

最佳答案

一个测试用例有四个不同的阶段:

  1. 设置
  2. 运动
  3. 验证
  4. 拆除

其中一些阶段可能是空的。例如,如果您使用 ARC,大多数拆卸会自动发生。

开始时,不要将任何内容放入 setUptearDown 方法中。只写一个单元测试。这是一个有效的例子。 (我要更改名称,因为 Objective-C 习惯用法是不使用“get”这个词。因此,我们将其称为 flag 而不是 getFlag。)我打算将类称为“示例”,我将使用 ARC。我使用缩写“sut”来表示“被测系统”。

- (void)testFlagGivenOneEntry
{
// set up
Example *sut = [[Example alloc] init];
[sut setTags:@{ @"key1" : @"value1" }];

// execute & verify
STAssertEqualObjects([sut flag], @"key1=\"value1\"", nil);
}

这是一个测试。让我们再添加一个。

- (void)testFlagGivenTwoEntries
{
// set up
Example *sut = [[Example alloc] init];
[sut setTags:@{ @"key1" : @"value1",
@"key2" : @"value2" }];

// execute & verify
STAssertEqualObjects([sut flag], @"key1=\"value1\"&key2=\"value2\"", nil);
}

此时,我们有重复的代码:sut 的创建。现在我们可以将变量提升为类的实例变量。然后我们在setUp中创建它并在tearDown中销毁它:

@interface ExampleTest : SenTestCase
@end

@implementation ExampleTest
{
Example *sut;
}

- (void)setUp
{
[super setUp];
sut = [[Example alloc] init];
}

- (void)tearDown
{
sut = nil;
[super tearDown];
}

- (void)testFlagGivenOneEntry
{
[sut setTags:@{ @"key1" : @"value1" }];
STAssertEqualObjects([sut flag], @"key1=\"value1\"", nil);
}

- (void)testFlagGivenTwoEntries
{
[sut setTags:@{ @"key1" : @"value1",
@"key2" : @"value2" }];
STAssertEqualObjects([sut flag], @"key1=\"value1\"&key2=\"value2\"", nil);
}

@end

有关更复杂的示例,请参阅 Objective-C TDD: How to Get Started .

关于iphone - 如何编写 OCUnit 测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14393768/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com