iOS Testing: Is there a way to skip tests?

心不动则不痛 提交于 2020-01-02 00:39:12

问题


I don't want to execute certain tests if the feature is currently disabled. Is there a way to "skip" a test (and to get appropriate feedback on console)?

Something like this:

func testSomething() {
  if !isEnabled(feature: Feature) {
    skip("Test skipped, feature \(feature.name) is currently disabled.")
  }

  // actual test code with assertions here, but not run if skip above called.
}

回答1:


You can disable XCTests run by Xcode by right clicking on the test symbol in the editor tray on the left.

You'll get this menu, and you can select the "Disable " option.

Right clicking again will allow you to re-enable. Also, as stated in user @sethf's answer, you'll see entries for currently disabled tests in your .xcscheme file.

As a final note, I'd recommend against disabling a test and committing the disabling code in your xcscheme. Tests are meant to fail, not be silenced because they're inconvenient.




回答2:


Another possible solution which I found in some article: prefix your skipped tests with something like "skipped_"

Benefits:

  • XCode will not treat them as tests
  • You can easily find them using search
  • You can make them tests again, replacing "skipped_" to ""



回答3:


I've found a way to do this by modifying my ui test .xcscheme file and adding a section called SkippedTests under TestableReference, then adding individual Test tags with an 'Identifier' attribute with the name of your class and test method. Something like:

<SkippedTests>
       <Test Identifier="ClassName/testMethodName" />
</SkippedTests>

Hope this helps




回答4:


Unfortunately, there is no build-in test case skipping. The test case either passes or fails.

That means you will have to add that functionality by yourself - you can add a function to XCTestCase (e.g. XCTestCase.skip) via a category that will print the information into console. However, you will have to put a return after that to prevent the other asserts from running.




回答5:


This is what test schemes are meant to do. You can have different schemes targeting different testing situations or needs. For example, you may want to create a scheme that runs all your tests (full regression scheme), or you may want to select a handful of them to do a quick smoke test on your app when small changes are made. This way, you can select different schemes according to how much testing you need to do. Just go to

Product >> Scheme




回答6:


There is no test case skipping. You can use if-else block:nested and run/print your desired output.




回答7:


It's not that universal, but you can override invokeTest in XCTestCase and avoid calling super where necessary. I'm not sure about the appropriate feedback in console though.

For instance the following fragment makes the test run only on iOS Simulator with iPhone 7 Plus/iPad Pro 9.7"/iOS 11.4:

class XXXTests : XCTestCase {

    let supportedModelsAndRuntimeVersions: [(String, String)] = [
        ("iPhone9,2", "11.4"),
        ("iPad6,4", "11.4")
    ]

    override func invokeTest() {
        let environment = ProcessInfo().environment
        guard let model = environment["SIMULATOR_MODEL_IDENTIFIER"], let version = environment["SIMULATOR_RUNTIME_VERSION"] else {
            return
        }
        guard supportedModelsAndRuntimeVersions.contains(where: { $0 == (model, version) }) else {
            return
        }
        super.invokeTest()
    }


来源:https://stackoverflow.com/questions/30478030/ios-testing-is-there-a-way-to-skip-tests

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!