Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Cypress and the Database

Cypress and the Database

We all know the problem. A test causes some changes in our database. These changes can break the following tests. Not the nicest thing to do.
Unfortunately, we can't just always mock the database as we can in Unit Tests. So we have to find other ways.
This talk is about various strategies for how to set up our tests so that they have the least possible effect on others.

https://www.youtube.com/watch?v=UApkfABawpg

https://github.com/rainerhahnekamp/cypress-database-patterns

Rainer Hahnekamp

April 20, 2021
Tweet

More Decks by Rainer Hahnekamp

Other Decks in Technology

Transcript

  1. Cypress DE Meetup, 14.4.2021 Rainer Hahnekamp Cypress & Database "The

    Elephant in the Room" 14.4.2021 Rainer Hahnekamp https://www.flickr.com/photos/instantvantage/9511539458 https://www.youtube.com/watch?v=UApkfABawpg https://github.com/rainerhahnekamp/cypress-database-patterns
  2. Cypress DE Meetup, 14.4.2021 Rainer Hahnekamp About me... • Rainer

    Hahnekamp • Trainer & Consultant for Angular • Full-Stack Developer @RainerHahnekamp
  3. Cypress DE Meetup, 14.4.2021 Rainer Hahnekamp Database Test #5 Test

    #1 Test #2 Test #3 Test #4 Indirect "Transitive" Coupling
  4. Cypress DE Meetup, 14.4.2021 Rainer Hahnekamp DB-Mock Test #5 Test

    #1 Test #2 Test #3 Test #4 Copy Strategy from non-E2E??? DB-Mock DB-Mock DB-Mock DB-Mock
  5. Cypress DE Meetup, 14.4.2021 Rainer Hahnekamp Issues with Test Seed

    • "One size fits all" approach • Tight Coupling → Not scalable • Fast Reseeding not always possible • Multiple Databases • Data from External Systems → no Seeding possible
  6. Cypress DE Meetup, 14.4.2021 Rainer Hahnekamp Individual Context - Best

    Case Scenario • Data is referenced to a particular entity ◦ User ◦ Product ◦ … • Multi-Tenant Systems • Customer-Centric Systems ◦ Insurances ◦ Banks
  7. Cypress DE Meetup, 14.4.2021 Rainer Hahnekamp Table "Users" ID |

    Name | ... 1 | Eva | ... 2 | Martin | ... Table "Diary" Table "Booking" .. | .... | ...
  8. Cypress DE Meetup, 14.4.2021 Rainer Hahnekamp Table "Users" ID |

    Name | ... 1 | Eva | ... 2 | Martin | ... 3 | Max | ... Table "Diary" Table "Booking" .. | .... | ... Test 1 "Add Diary"
  9. Cypress DE Meetup, 14.4.2021 Rainer Hahnekamp Table "Users" ID |

    Name | ... 1 | Eva | ... 2 | Martin | ... 3 | Max | ... Table "Diary" Table "Booking" 4 | Lucy | ... Test 1 it('should add a new diary', …); Test 2 it('should start the tutorial on empty diaries', …) .. | .... | ...
  10. Cypress DE Meetup, 14.4.2021 Rainer Hahnekamp API Arrange Possibilities: Normal

    Requests • Default Case • Call same endpoints as the Frontend • Don't use the frontend directly! • cy.task() as alternative
  11. Cypress DE Meetup, 14.4.2021 Rainer Hahnekamp API Arrange Possibilities: Dedicated

    Test API • Backend provides special API for test mode • Shortcuts possible, e.g. ◦ merge chain of requests into one ◦ Overcome Security Issues • Best Option
  12. Cypress DE Meetup, 14.4.2021 Rainer Hahnekamp Testing Context • Individual

    Context ◦ All data depends on a certain ID ◦ e.g. Personalised Data ◦ Best Option in Combination with Test API (Sign Up & In) • Global Context ◦ Tests Affect each other ◦ Challenging Parallel Runs ◦ Not so easy to solve...
  13. Cypress DE Meetup, 14.4.2021 Rainer Hahnekamp Global Context Patterns I:

    Independent Tests • Read-Only Character • No Arranging required • Rely on Test Seed • Smoke Tests • Tests for Static Elements
  14. Cypress DE Meetup, 14.4.2021 Rainer Hahnekamp Global Context Patterns II:

    Intelligent Tests • "I'll create and find it" • Flexible • Requires more code
  15. Cypress DE Meetup, 14.4.2021 Rainer Hahnekamp Global Context Pattern III:

    Dependent Test Group • Default Group • Logical Group of Unit Tests • Internal knowledge about other tests • Order is important • Database Reset after each Group Run
  16. Cypress DE Meetup, 14.4.2021 Rainer Hahnekamp Global Context Patterns IV:

    Simulated Individual Context • Mock all APIs • Transforms a global into an individual context
  17. Cypress DE Meetup, 14.4.2021 Rainer Hahnekamp Arrange Act & Assert

    Test Seeded DB Application API Test API 😍 😭 Independent Intelligent Dependent Test Groups Simulated Local Context Global Context Individual Context 😍 😭
  18. Cypress DE Meetup, 14.4.2021 Rainer Hahnekamp Summary • You will

    not have completely isolated tests • Try to minimize loose coupling • Always prefer Backend API over Test Seed • Look out for opportunities with individual context