Change the mock object with a command line argument

The UI Testing is powerful function that Xcode has.

The UI Testing is the one of the unit test, it generate events such as the mouse down event or a key down event, the application responds to them same as user operations. It tests the user interface such as a window content.

How ever, it is hard for me. For example, the mock.

The mock is used to test functions that are hard to automatically test. For example, the function to communicate with a web server or the real devices. The mock returns results that the application want without real communication with the real servers or devices..

You need to test functions that are replaced with the mock, but it is not important when you test the user interface.

To use the mock, the application use a protocol instead of a real class. For example, when the application communicates with the web server which has APIs, create the API protocol, the real class is adopt to this protocol. Also, the mock is adopt to this protocol, then you can use the mock when you test.

There is a problem. How to replace with the mock?


Use a command line argument

The UI Testing code launch the testable application in the test code, then you can pass the command line argument to the launched program. The launched program check the command line argument, replace with the mock.

For example, see following code.

var app = XCUIApplication()
app.launchArguments = ["--mock_device"]

This code pass the command line argument “–mock_device” to the testable application, the testable application replace the device class with the mock device class when the command line argument contains “–mock_device”.

How about the UI Testing

At this time, I don’t know whether the UI Testing is useful or not yet. Creating the test code and the mock are hard work. It consumes the time. However, if it shrinks the TCO in the system test, then I will think it is very useful.

When I had been changed the code, it was caused to break the function that I couldn’t figure out, but the UI Testing had been detect it. I learned the UI Testing is very powerful.

Updated on February 21, 2020.

After a bit of work, I found it to be quite effective. There was more than I thought to make sure that internal structure changes weren’t affecting the GUI. I made improvements so often that I didn’t even notice.