Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write an automated unit test of a GUI in Xcode?

I want to write a unit test of just the GUI part of my Cocoa application.

In the textbook unit test, there's a test framework and test case that calls the unit under test. All the code below that unit is mocked out. So, both the input and the output are controlled and monitored; only the code in the unit under test is tested.

I want to do the same thing where the unit under test is my GUI:
1) Set up some kind of framework where I can write code that will manipulate and inspect GUI controls.
2) Connect my GUI controls to mocks of my actual code, not to the real instances.
3) Run the test, which manipulates the controls and then checks the mock object to see whether the correct methods were called with the correct parameters and checks the GUI to see whether the responses from the mock object causes the correct changes in the widgets.

Anyone doing this? If so, how? Any ideas on how I could do this?

Thanks,

Pat

(Edit) To give a very specific example, I want to:
1) Write a test case that will select the menu item 'MyMenu' -> 'MyItem'. In this test case, I want to check to see that the method [AppDelegate doMyItem] gets called precisely once and that no other methods in AppDelegate get called.
2) Generate a mock object of AppDelegate. (I know how to do this)
3) Somehow (handwaving here) link my application so that a mock instance of AppDelegate is linked in instead of the real one.
4) Run the test. Watch it fail because 1) I haven't created MyMenu yet. 2) I haven't created MyItem yet. 3) I haven't done the IB work to connect MyItem to [AppDelegate doMyItem], or 4) because I haven't written the 'doMyItem' method yet.
5) Fix the above four issues (one at a time if I'm feeling really pedantic that day).
6) Run the test again and watch it succeed.

Does this make the question clear?

like image 734
Pat McGee Avatar asked Jun 22 '09 19:06

Pat McGee


People also ask

Can GUI testing be automated?

Automated GUI Test Tools Ranorex: Ranorex is an excellent tool that allows automation for multiple platforms, all under one software package. It's a good solution for implementing automated testing in continuous delivery and DevOps environments, demanding continuous testing and fast feedback to testing results.

How do I test UI in Xcode?

If your app doesn't already have a UI testing target, all you have to do to add one is to go to File > New > Target.. in Xcode and select a “UI testing bundle”.


1 Answers

Two principles, two links:

  • Make the view as dumb as possible, with the passive view pattern: this makes GUI easier to test
  • Trust but verify: Trust Cocoa implementation of buttons, menus, ... But verify that target and action are correctly connected, that bindings are as expected.
like image 152
mouviciel Avatar answered Oct 13 '22 13:10

mouviciel