 # Unit tests in Drupal

 

 

Date: Monday, March 1, 2021

 By  [CivicActions](/civicactions)

### Tips on getting started with test-driven development for cleaner, faster code

Photo by[ Markus Spiske](https://unsplash.com/@markusspiske?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on[ Unsplash](https://unsplash.com/?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)

At [DrupalCon Global 2020](https://events.drupal.org/global2020), CivicActions Engineer [Eric Napier](https://civicactions.com/team/eric-napier) and Open Data Expert [Gerardo Gonalez](https://civicactions.com/team/gerardo-gonzalez) discussed Test-Driven Development (or TDD) for writing clean code that works in Drupal. TDD is an approach to software creation that requires engineers to write tests for code that doesn’t yet exist, in order to create a continuous feedback loop between the code and the developer(s).

TDD first came about as a response to outdated and inefficient software development processes such as [Waterfall](https://www.projectmanager.com/waterfall-methodology). While it might feel unnatural to test something first without knowing whether or not it will work, this presentation aims to take the fear out of Test Driven Development. Watch as our engineers give live demos of this process, along with the following:

- The history of TDD (Test-Driven Development)
- Why TDD works
- How to write Awesome Unit Tests
- How to apply TDD in Drupal
- When to use different types of unit tests

[Watch “Unit Tests in Drupal” from DrupalCon Global 2020](https://www.youtube.com/watch?v=UGg3G0dsAGw)

“At its core, TDD is a very simple process: write a test first, see the test fails, write just enough code to make the test pass, and then let the test drive your workflow.” — Eric

**Highlights**

- 1:00 — Overview of TDD and how it was first used by NASA on [Project Mercury](https://www.nasa.gov/content/the-history-of-mercury-mission-control/#.YCWW9WgZ6Et)
- 7:03 — How CivicActions uses Agile and TDD practices to bring value to client work
- 9:03 — The two rules and three phases of TDD
- 10:36 — How to run tests using PHPUnit
- 11:17 — Creating the memory store
- 12:33 — Retrieving the strings from the store
- 13:37 — How to run a completed test (and what to do when it fails)
- 17:45 — Viewer participation helps a test pass when an unknown issue arises
- 25:43 — Defining and demonstrating the difference between Solitary and Sociable tests
- 30:00 — Gerardo’s [Mockchain Library](https://github.com/GetDKAN/mock-chain), and how it helps users working in PHPUnit
- 35:37 — How to run these tests in Drupal, and the common challenges you might face
- 40:10 — Live demo test in Drupal, and the results

**Resources**

- [Presentation Slide Deck](https://docs.google.com/presentation/d/1CbCw55xm5WKE8MAhcw7J7A5tclFG39fEa5hxLGtejPM/edit)
- [Presentation Overview](https://events.drupal.org/global2020/sessions/unit-tests-drupal-road-test-driven-development)
- [More About Mockchain](https://www.drupalgovcon.org/2020/program/sessions/mock-chain-library-easier-unit-testing-drupal-and-beyond)
- [Kent Beck’s Book](https://www.goodreads.com/book/show/387190.Test_Driven_Development?ac=1&from_search=true&qid=fhDzjvpejn&rank=1)

**Connect with Eric Napier**

- [LinkedIn](https://www.linkedin.com/in/eknapier/)
- [GitHub](https://github.com/eknapier)
- [Drupal](https://www.drupal.org/u/ericnapier)
- [Email](mailto:eric.napier@civicactions.com)

**Connect with Gerardo Gonalez**

- [LinkedIn](https://www.linkedin.com/in/fmizzell/)
- [GitHub](https://github.com/fmizzell)
- [Drupal](https://github.com/fmizzell)
- [Email](mailto:gerardo.gonzalez@civicactions.com)