Can a UI application be unit tested? Lets consider a UI application which has a simple login form. What would form part of unit test for this UI application. Should the backend response validation form part of unit test for login form?
In my opinion, It should include;
- appropriate rendering of UI components in the form.
- enabling/Disabling of components based on user actions (password can not be empty message when password is not entered and form is submitted).
Are there any guidelines/rule of thumbs which should be used while devising unit test cases for UI application? Should an application behavior considered in unit testing?
UI testing is not unit testing in the strict sense, however there are tools/frameworks to help that. I assume you mean a web UI – for this you can try e.g. Selenium or HttpUnit.
Note that “appropriate rendering of UI components” is a very loose term – it can mean things which can not be verified automatically, only by human eye.
Testing functionality via the UI is always more cumbersome than testing the code directly via classic unit tests. Hence it is always recommended to separate the business logic from the UI as much as possible – then you can thoroughly test all the nooks and crannies of the logic itself via unit tests, and need only integration tests on the UI to verify that the app as a whole works as expected.
In your example, ideally the input validation and the authentication would be separated from the GUI, so you could unit test these (preferably by mocking out the DB). Thus you could verify that the login works as expected for all sorts of normal/abnormal inputs (empty userid/password, strange characters, very long input, logging in the same user twice, …). Then in the UI tests you need only test a few common test cases to verify that the app indeed is functional.