Unit testing graphical user interfaces

Once you start using BDD (or TDD (whenever I write BDD I could have written TDD) one common obstacle you encounter is writing tests for your GUI. Most people I know feels it is so hard that they actually don't write tests for their GUI. Instead they keep the GUI layer of the application as thin as possible and reduces the number of defects there that way. Others use tools like Selenium to add tests once the GUI is created. In both cases you're not really being a strict BDD practitioner.

Actually I think this is a very common, pragmatic decision made by many developers since most people find GUIs very hard to test. But are they really? I recently looked at a company internal web cast on testing where the speaker said something like: "I've written unit tests for user interfaces most part of my career. When I started nobody told med unit testing user interfaces was hard so I just did it". This is a very important observation. If you are a convinced BDD practitioner I think you believe that the use of BDD will lead to a better and more testable design. You have also probably experienced how BDD have changed the way you design your code as compared to before you started with BDD and did everything the old fashioned way. So in other areas you have evolved the way you design your code. But still you write your GUI the same way you did before.

Why do we stick with the same old GUI code then (other than that everybody keeps saying it is impossible)? I think our development tools are one problem here. Our tools are used to design our GUIs and generates lots of code. And that code is not testable so if we want to write testable code we believe we have to write more code manually. And writing GUI code manually is boring, right?

Albert Einstein once said: "We can't solve problems by using the same kind of thinking we used when we created them". And I think that is very true for this case too. If we want to write unit tests for our GUIs in a BDD manner I think we must rethink how we write our GUIs. One way of doing it is described in this lightning talk which is in Swedish.

Comments (1)

  1. When I last wrote about TDD and GUI I was talking quite vaguely about how TDD of GUIs can work. Since

Skip to main content