Every situation is different, but these are a few tips that will help make automated UI testing easier:
- Make the UI as thin as possible. Don't put business logic in UI components, even though it can be tempting (patterns such as Model-View-Controller help with this). An example:
Difficult to test:
JButton button = new JButton("Check if even");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int num = Integer.parseInt(numberField.getText());
if ( num % 2 == 0 ) {
System.out.println("num " + num + " is even");
}
}
});
This is difficult to test because now the whole UI needs to be brought up just to test this function, and this is not conducive to automated testing. This can be refactored as follows:
JButton button = new JButton("Check if even");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int num = Integer.parseInt(numberField.getText());
if ( NumberUtils.isEven(num)) {
System.out.println("num " + num + " is even");
}
}
});
public static class NumberUtils {
public static boolean isEven(int num) {
return num %2 == 0;
}
}
While this may not look like a big deal, the change is signficant. The NumberUtils class can have a simple unit test written for it and now that code is being tested outside the UI. It's easy to see that as functions become more complex, it becomes even more important to move them outside the UI logic. - Wait until the last responsible moments to write UI tests. Don't write automated UI tests while the UI is still in a great state of flux. You'll spend more time maintaining them than you will find useful. This will vary from project to project, but I find this doesn't generally happen until later on in the project.
- Maintain automated UI tests like you maintain code. They should be run as part of a regression test (even if it's a nightly test as opposed to a continuous build since they may take longer to run). They generally can't be simple record/playback tests. While this is a good place to start, they really should be refactored by a developer to make sure that when the UI changes and tests break, minimal changes will be needed to the UI tests. For example, if multiple tests are looking for a particular table column, make that a static string somewhere (or even reference it from the code under test if you can and the string itself is not under test). This way if the string changes, you don't need to update the string in multiple places.
No comments:
Post a Comment