How do I access my swift classes from my UI tests?

痞子三分冷 提交于 2019-12-08 04:32:51

问题


I have a UI test like so :

    func testHome(){
         if(isRedOrange.clear()){
                //code
            }
    }

How would I access my isRedOrange.clear function from my isRedOrange.swift file from my UI tests?


回答1:


UI Tests are black boxed, so you cant have access to your code.

You can use @testable import in Unit Tests, so full access will be provided. When you're running UITests this is not working, because during a UITest your test class cannot access your app's code.

From Apple's Docs:

UI testing differs from unit testing in fundamental ways. Unit testing enables you to work within your app's scope and allows you to exercise functions and methods with full access to your app's variables and state. UI testing exercises your app's UI in the same way that users do without access to your app's internal methods, functions, and variables. This enables your tests to see the app the same way a user does, exposing UI problems that users encounter.

You must achieve everything using .tap()'s on elements. .accessibilityIdentifier will help you to get the right element




回答2:


You need to import your main module (project) into tests:

  • Ensure you've set ENABLE_TESTABILITY in Build Settings of the main project target to true.

  • To import it into tests, call @testable import MAIN_TARGET_NAME in your UITests file.




回答3:


Goto projects settings -> Select uitests target -> build phases tab -> add your swift file to compile sources




回答4:


The question makes no sense. You don’t access any of your app’s code in a UI test. If you want to access your code, write a unit test.



来源:https://stackoverflow.com/questions/51799940/how-do-i-access-my-swift-classes-from-my-ui-tests

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