Lessons learned automating Drupal 7 tests with Behat and TravisCI
- Is It Worth the Time? It was, we were at about 6hrs/week (and it took about 2 months of billiable time).
- We had a growing team.
- Impressive suite of behat tests (first phase of automating tests; before my time).
- Increasing nervousness over the potential for problems and opportunities for human error.
- Clients ready to pay for tests to become part of their definition of done.
- We had resources for training (I was new) and infrastructure.
- Be free and we didn't want to host the CI application.
- Integrate with GitHub.
- Build our products.
- Work with existing tests.
- Be easy to update scripts across dozens of different repositories.
- Site looks professional.
- Data is accurate and complete.
- Layout and organization are as planned.
- Site functions (for admins and editors) as expected.
- Reduce the time between development (or security announcement) and deployment.
- Reduce the amount of code changes tested at once, so that we can find and fix bugs more quickly.
- Reduce the amount of time we spent running tests manually.
- Include test results as part of our code review process, without the added burden of manually running tests per pull request.
- Shift from "write more tests" to "all tests must pass" and what breaks better feel worth fixing, either in your code or the test.
- Prototyped build and test on TravisCI.
- Incorporated round of developer feedback.
- 2-week sprint to clean up tests and add Travis to client's top 10 modules.
- Repeated with another client.
- Added tests to definition of done for custom work going forward.
Module Development: Local > GitHub Pull Request > CodeClimate > Travis/Behat
Make File Version Upgrades: Update Versions in Make Files > GitHub Pull Request > CodeClimate > Travis/Behat
Production Code Upgrades: Copy over code from Profile Builds > Manually Deploy to UAT > Manually Test with Behat > Manually Deploy to Production > Manually Test with Behat
- Because Behat tests emulate keyboard navigation, they help us test for the accessibilty of our sites.
- Widespread adoption by the Drupal community.
- Sensible defaults, ie. never had to adjust MySQL max_packet_size, came with Firefox, etc.
- Prints out container defaults.
- Free for open source projects; if we had to pay, not based on disk space (which is harder to control).
- Allows secure environment variables for public repositories, so we don't have to store credentials in code.
- Their favicon indicates build status, which won my heart. They get that these processes should and will be running in the background of our browser.
- Able to get a local copy up and running pretty quickly.
- I was able to find many public repositories using TravisCI with Drupal and Behat, as well as lots of good documentation.
- Not testing the origination branch of a pull request.
- Not running all tests for a module or product. It still passes, but isn't testing all that we wanted it to test.
- Not setting up a local copy of the CI tool right away. If you're troubleshooting runserver or file paths, no point in waiting for an entire site to build each time you want to try something new.
- If you're running tests within a script, so not straight from a command in the .yml file, then be sure you force a failing exit code if at least one test fails (example).
- Written fewer Behat tests.
- Run all tests against our most basic product, and not against all of our products.
- Added TravisCI to fewer repositories.
- Exclude Javascripts tests entirely, which means we wouldn't have needed to setup Selenium and xvfb.
- Focused on testing integrations (integrations are an important part of the value we offer).
- If we had a pattern library or style guide, try shoov for visual regression testing.
- Automate tests for individual, custom client sites, ie. not just our modules and products.
- Automatically update product make files when new module release is tagged (that would be really nice!).
- Rewrite a number of our tests, so that they perform functionality not just check if fields and text can be found.
- Reorganize tests, so that they travel with their related code, instead of being housed in a single test repository.
- Automate deployment of changes to UAT sites.
I'd recommend watching Lynda.com's DevOps Fundamentals Course.