End-to-end Testing with Cypress Series: 06 DRY (Don't Repeat Yourself)
- Publish Date
- Authors
- Jeremy Fairbank
- Location
- Maui, HI, USA
Transcript
In this video, learn about the DRY (Don’t Repeat Yourself) principle and how it can affect end-to-end tests. After watching this video, you will be able to create reusable helpers for data setup in your own Cypress end-to-end tests.
- 00:00
- (upbeat music)
- 00:03
- Welcome back
- 00:04
- to Test Double's intro course
- 00:06
- on end-to-end testing with Cypress.
- 00:08
- In the previous video,
- 00:09
- you added another real world happy path test,
- 00:13
- learning about other useful commands in Cypress,
- 00:16
- and why not to share persisted state between tests.
- 00:19
- In this video,
- 00:20
- you will learn about the DRY principle,
- 00:24
- learn how duplication can affect tests,
- 00:27
- and reduce some duplication
- 00:28
- in the existing yams test suite.
- 00:31
- DRY is a common acronym used in software development.
- 00:35
- It stands for, "Don't Repeat Yourself."
- 00:38
- The basic premise behind it
- 00:40
- is to make code more maintainable
- 00:42
- by reducing duplicated code and concepts.
- 00:45
- When you need to make a change
- 00:46
- to a piece of duplicated code,
- 00:48
- you have to update that code
- 00:50
- in all the places it's replicated.
- 00:52
- That takes more time
- 00:53
- and can lead to unintended bugs
- 00:55
- if you miss updating a place.
- 00:58
- So developers use various techniques
- 01:00
- to manage code needed in multiple places
- 01:03
- with abstractions that reduce duplication
- 01:06
- and are more maintainable.
- 01:08
- Whether it's duplicated setup code
- 01:10
- or redundant tests,
- 01:12
- duplication poses problems in tests as well,
- 01:15
- making test code harder
- 01:16
- to maintain and understand
- 01:18
- as well as slowing down test suites.
- 01:21
- To see what I mean,
- 01:22
- let's fix one glaring example of duplication
- 01:25
- in the current yams test suite.
- 01:27
- Boot up the back-end server Cypress environment,
- 01:30
- front-end dev server, and Cypress itself.
- 01:34
- Also, ensure you're running
- 01:35
- the tasks.spec.js file in Cypress.
- 01:40
- Inside the spec file,
- 01:41
- you have three spots
- 01:42
- where you duplicate the same code
- 01:44
- to generate a unique task title
- 01:47
- by appending a timestamp.
- 01:48
- Let's extract that logic
- 01:50
- into a reusable function
- 01:52
- so you don't have to repeat it everywhere.
- 01:54
- At the top of the test file above describe,
- 01:57
- add a helper function called uniqueTitle.
- 02:00
- The function accepts the title as an argument
- 02:03
- and returns a version of the title
- 02:05
- with the current timestamp appended.
- 02:08
- Update the spots
- 02:09
- where you manually add the timestamp
- 02:10
- to use this function,
- 02:16
- and then save the file
- 02:17
- to make sure the tests still pass.
- 02:20
- Now, if you ever wanna update the core logic
- 02:22
- of generating a unique title,
- 02:25
- you'll only need to make that change in one place.
- 02:28
- Let's recap what you learned in this video.
- 02:31
- You learned about the DRY principle,
- 02:33
- you learned how duplication affects tests,
- 02:36
- and you reduced some duplication
- 02:38
- in the existing yams test suite
- 02:40
- by creating a reusable function
- 02:42
- to generate unique tasks titles for tests.
- 02:45
- You are now ready to DRY up your own tests,
- 02:48
- but let's take your knowledge further.
- 02:50
- In the next video,
- 02:51
- you will learn how redundant tests
- 02:53
- can violate the DRY principle
- 02:55
- and slow down test suites.
- 02:58
- You will discover how to combine tests
- 03:00
- to reduce duplication and speed up tests.
- 03:03
- (upbeat music)
‹ End-to-end testing with Cypress series: 05 Isolate test state
Jeremy Fairbank
- Code Name
- Agent 0029
- Location
- Maui, HI