Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS Why Unit Test a class calls ViewController

I wrote a unit test like below:

class Test445Tests: XCTestCase {
    func testPerformanceExample() {
        XCTAssert(true)
    }
}

When the test runs, something calls ViewController.viewDidLoad! Can someone explain why this happens?

class ViewController: UIViewController {

    static var s = false

    override func viewDidLoad() {
        super.viewDidLoad()
        ViewController.s = true // put the debugger here.
    }


}

If you write this code and put a debugger on ViewController, you can check that the debugger will stop on that line.

like image 639
Saba Avatar asked Nov 07 '18 19:11

Saba


People also ask

What is difference between unit tests and UI test in Xcode?

While unit testing seeks to create a rapid and regular feedback loop for developers to gain confidence in correctness of the code, UI testing validates the application as a whole from an end user's perspective to ensure that the final product performs as expected by users.

What should I unit test iOS?

A unit test is a function you write that tests something about your app. A good unit test is small. It tests just one thing in isolation. For example, if your app adds up the total amount of time your user spent doing something, you might write a test to check if this total is correct.

What is XCTAssert in unit testing?

XCTAssert is one of a family of asserts for unit testing from the XCTest framework, and should only be present in Unit Test Targets (i.e. not in your application code). If the assert fails, it does not terminate the execution of the test harness or hosting application, but records and reports the failure.

What is unit test class?

Unit Testing is a type of software testing where individual units or components of a software are tested. The purpose is to validate that each unit of the software code performs as expected. Unit Testing is done during the development (coding phase) of an application by the developers.


1 Answers

It's creating an instance of ViewController because your app creates a ViewController at launch, and your test bundle uses your app as its “Host Application”:

host application setting

When the setting is configured like shown above, it means Xcode runs the tests in your test bundle by launching the host application, then injecting the test bundle into it.

You can change the “Host Application” setting to “None”:

host application set to none

When the setting is configured as “None”, Xcode runs your test bundle using a non-GUI app called the XCTest agent, and doesn't launch your app, so your app doesn't get the chance to create a ViewController.

Note that when you set “Host Application” to “None”, your test bundle can no longer access any APIs defined in the application. Any APIs you want to test, you have to move into a framework, and link the test bundle with that framework (in the “Build Phases” tab for the test bundle).

Note also that even when you set “Host Application” to “None”, Xcode still launches a simulator. Your test cases are allowed to use iOS system services that are only available when a simulator is running, and Xcode doesn't know in advance whether your test cases need those services. So it has to launch the simulator first, just in case.

like image 89
rob mayoff Avatar answered Sep 20 '22 21:09

rob mayoff