iOS Testing: Is there a way to skip tests?

走远了吗. 提交于 2019-12-04 23:27:43

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.

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 ""

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

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.

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

Vicky

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

In the absense of build-in test case skipping, the cmd-/ Comment Selection command with some searchable pattern like :TEST:SKIP: reason can be simple and useful. Print a meaningful message to the console.

func testSomeFeature() {
    print(":TEST:SKIP: testSomeFeature disabled pending ...") 
//    ... skipped selection ..
}

Later, update someFeature and uncomment someFeatureTest at the same time.

Using some search pattern like :SKIP:, can allow all skipped features and tests to be found in source code and console output.

Note: cmd-/ '//' style comments preserves the code indentation after a Re-Indent command. The /* ... */ style comment can loose some readability since the code indents are not preserved.

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