Confused
Confused

Reputation: 3926

How to call a method in main target from UI target in XCTest?

Before asking the question, I should say this. My understanding about UI Testing in Xcode:

My scenario:

I want to call a method (Say myMethod()) in the view controller (Say MyVC.h/MyVC.m) in my source code from my UI Test case.

Note: My source code was written in Objective C, and I am writing test cases in Swift.

From my understanding, to do this, I should add MyVC.m(We can not add .h to the target) in UITest target also. Then I need to import MyVC.h in my UITest bridging file. Then I can call myMethod() from UITest like this:

MyVC.myMethod()

It will work!!

The problem:

In real time, most of the classes use Util classes, Constant files(Header files), classes that was imported from .pch file, Shared managers, etc.

If MyVC is a independent class, means which will not use any of the classes said above, I can access myMethod() without any problem.

But, if MyVC uses those classes indirectly, running UITest shows missing header files errors in all source code.

What I tried:

Question:

Should I add all needed files header in all files of main source?

Have anyone faced this type of error? Did anyone done DB access from UI Testing?

Upvotes: 1

Views: 1692

Answers (1)

Jeremy W. Sherman
Jeremy W. Sherman

Reputation: 36143

Two things:

  • The latter half of your question seems to deal with the build problems. For those, yes, you need all dependencies in the target.
  • The title of the question asks about calling a method in the tested app from a UI test. UI tests run in a different process from the app, so you can't just access an object in the app. You need to either move the trigger in-band by adding accessible UI, or rig up a side channel, which might be doable using say a local socket.

I think the second bit might not matter for your needs, since from the bit about hitting a database from the UI test makes it sound like you don't need the specific object in the app, just an object of that class to get at some DB access methods that are declared on it for some reason. For that, just being able to compile in the class and instantiate and message it suffices. (You might still make your life easier by factoring those DB-related methods out to a non-UI class, though.)

Upvotes: 1

Related Questions