【问题标题】:Get name of test method in setUp (Xcode)在 setUp (Xcode) 中获取测试方法的名称
【发布时间】:2016-08-15 05:32:39
【问题描述】:

我想在日志中附加一条消息,其中包含即将运行的测试方法的名称。我想在我的测试超类的 setUp 方法中执行此操作,这样我就不会到处都有重复的代码。

我想做这样的事情:

- (void) setUp {
    [super setUp];
    [self log:@NSStringFromSelector(_cmd)];
}

但是,_cmd 总是将“setUp”作为其字符串,而我想要“test00TestTheThing”

有没有办法做到这一点?

【问题讨论】:

    标签: objective-c xcode xctest


    【解决方案1】:

    不知道它是否仍然相关,但如果有人需要它:

    您可以使用self.name,然后您将获得带有测试名称的类的名称。

    要删除类名并保留测试名称,请使用replacingOccurrences func

    示例:

    class ClassName: XCTestCase {
        override func setUp() {
          // get the name and remove the class name and what comes before the class name
          var currentTestName = self.name.replacingOccurrences(of: "-[ClassName ", with: "")
    
          // And then you'll need to remove the closing square bracket at the end of the test name
    
          currentTestName = currentTestName.replacingOccurrences(of: "]", with: "")
        }
    

    【讨论】:

      【解决方案2】:

      我发现了这个:

      self.name
      

      但是,这给了我“-[AppUITests test00TestTheThing]”

      【讨论】:

      • 看起来这就是您想要的 - 它是该测试的唯一标识符。如果您只想要测试的名称,您可以使用正则表达式从您获得的字符串中提取它,但我想说拥有所有信息很有用,即使它只是在您需要调查时快速导航到测试它。
      猜你喜欢
      • 2012-09-24
      • 1970-01-01
      • 1970-01-01
      • 2011-05-29
      • 1970-01-01
      • 1970-01-01
      • 2012-11-10
      • 2011-02-26
      • 2017-01-27
      相关资源
      最近更新 更多