I’ll be honest: I don’t love coding. Don’t get me wrong, I love test automation! I love the feeling of solving a technical challenge and coming up with a great way to automatically assert that software is doing what it’s supposed to be doing. I love maintaining and updating my automated test suites. But the actual writing of the code is not my favorite thing. Whenever I find myself having to write another nested “for” loop, I sigh inwardly.
However, with all the coding I’ve done over the years, I’ve come to really appreciate the work that software developers do! Software is complex stuff, and developers have come up with great ways to set standards, share repositories, and review each other’s work.
The test automation code we write is important; just as important as the code the software developers are writing. Therefore, we should write our code with the same standards the developers use. Here are a few suggestions for coding practices you should adopt:
- Keep it simple. Always look over your code and ask yourself if there’s a simpler way of doing what it is that you are trying to do. Sometimes the obvious solution to a testing problem only becomes clear after you have solved it in a complicated way; now it’s time to go back and solve it more elegantly.
- Don’t repeat yourself. If there’s something you’re doing in more than one test- for example, logging in to the application- write a method that you can call instead of putting those steps into every test. Similarly, create a file where you save all of your variables and element locators, and have all of your tests refer to that file. That way if a variable or a locator changes, you can make the change in one place rather than several.
- Be consistent. Consistent code is easier to read. Be consistent with your casing: if you have a variable for the user’s first name called “firstName”, don’t make the variable for the user’s last name “LastName”. Follow the conventions that your developers are using: if they indent with two spaces, you should too. If they put their opening curly braces on a separate line, you should as well.
- Comment your code. It’s not always obvious what test automation code is doing at first glance, and while you might be quite used to the syntax you are using for your tests, your developers might not be familiar with it. Simple comments like “Polling the queue for the delete request” can be really helpful in explaining your intent. Moreover, what might seem really obvious to you now might not be obvious in three months when you need to update the test! Your future self will thank you for the comments you write today.