As a web developer or designer, you likely work on many of different websites and web applications; whether they be small brochure websites for a company or full-blown applications. One thing that can be hard to decide on is "do I need to write tests for this project?".
In this quickfire post, we're going to discuss how to determine whether you need to write tests. We'll also briefly explore what types of tests you might want to write.
When to Write Tests for Websites
When it comes to websites, there are a few different factors to deciding whether or not you need tests for it.
Let's imagine you're building a brochure website for a business that just has 'Home', 'About', 'Services' and 'Contact Us' pages. We'll make the assumption that the pages are fairly basic without any business logic, and that the 'Contact Us' page has a form that a visitor can submit.
In my personal opinion, if the form is just grabbing the form data and sending it as an email to the site admin, we probably don't need to write any tests for the site. If there are any errors with the form, they will likely be picked up during manual testing and when the client signs off on the project.
Of course though, this doesn't mean that you're not allowed to write tests. A few small browser tests can be really helpful just for extra peace-of-mind if needed. But it's important to remember that tests are usually there to ensure business value by reducing the time you spend finding and fixing errors. So, if there's not really much that can wrong with displaying your website, you might actually be costing your business more time and money than needed.
Let's take a look at the different scenarios where you might want to write tests for your website:
1. Dynamic UI
There may be times that you want to write tests for the form. Let's say that the form in the example above is dynamic and some fields are shown/hidden depending on another fields' value. This is something that you sometimes see on forms that are dealing with billing or shipping addresses. For example, if you select the 'United Kingdom' in the country field, a 'Postal code' field might appear. Whereas, if you were to select 'United States', a 'ZIP code' field might appear instead.
If your site has something like this, it might be a good opportunity to write some tests. With test frameworks like Cypress, it can be really easy to get up and running and write some tests in less than an hour. As a result of writing the tests, it can give you a bit more confidence that the form shows and hides fields as you'd expect.
2. Complex Business Logic
Similar to our example above, you might have some complex business logic in your website. Let's say that you have a contact form and after it's submitted, depending on the form values, you might want to make a call to a third-party service. For example, you might want the contact form values to be sent to a third-party service (such as Mailchimp or Zoho) for storing.
In this case, you might want to write a few small feature tests to assert that the necessary API calls are being made. If you're creating your project with PHP, you could write these tests using something like PHPUnit.
3. You Have to Keep Checking Manually
You might find that with some websites that you're working on that you feel the need to double-check and manually test the form each time you make any changes to it. I'll be honest, I can be a bit of paranoid developer sometimes and used to feel the need to check the forms on my site every every time I pushed any new changes up to my website (even if the changes were unrelated).
I worked out that I was spending around an hour a week manually testing my site to make sure that forms, filters, downloads and blog posts all worked as expected. To put that in perspective, that's 4 hours a month or 52 hours a year. That's 52 hours a year that I wasn't abe to work on other projects or create new content for my blog!
I spent around 3 hours writing some browser tests using Cypress and some integration tests using PHPUnit. Now that I have my tests, I can confidently push my changes up to my live website knowing that if my tests are passing, the chances are that my site will be okay. Of course, I still do some manual testing regularly though to make sure that everything's still in order.
So, if you find that you're doing the same as I was doing and doing a lot of manual checking that could be automated, I'd investigate the possibility of adding some tests to help you. Not only will it help to give you better peace-of-mind, it can also help you to save time.
Writing Tests for Applications
In my personal opinion, if you're building an application, such as a SaaS (software as a service), you should more-or-less always write tests. Unless the application is an MVP (minimum viable product), prototype, or a personal project that you're making for just yourself to use, I'd always recommend writing tests.
By writing tests for your application, you can save a lot of time (and money) for yourself and reduce the chance of frustrating your customers if something isn't working correctly.
Of course, if the personal project, MVP or prototype starting turning into production-ready applications that are other people are able to use, I'd recommend adding some tests.
For a bit more reading on the benefits of writing tests and how they can help you, I'd recommend giving my post about How to Make Your Laravel App More Testable a read. It covers why you should write tests for your applications and then shows how you can write tests for your Laravel applications.
Hopefully, this quickfire post should have given you an idea of how to decide when to write tests for your website or application. Please remember though, these points above are my personal opinion and other developers and designers will also have their own opinions on the subject.
If this post helped you out, I'd love to hear about it. Likewise, if you have any feedback to improve this post, I'd also love to hear that too.
If you're interested in getting updated each time I publish a new post, feel free to sign up for my newsletter below.
Keep on building awesome stuff! 🚀