Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated Testing #6

Open
Tracked by #5
coryhouse opened this issue Apr 18, 2018 · 0 comments
Open
Tracked by #5

Automated Testing #6

coryhouse opened this issue Apr 18, 2018 · 0 comments

Comments

@coryhouse
Copy link
Owner

coryhouse commented Apr 18, 2018

My Cypress vs Playwright Benchmark

102 tests in Chrome

On Jenkins, single core:
Cypress: 4:30
Playwright: 1:58

M1 MacBook Pro (multi-core):
Cypress: 2:15
Playwright: 20 seconds!

Best Practices

Data

  • Write atomic tests. Write the data you need for each test, then remove it. Avoid using the same data for multiple tests if the data can be changed.

Principles

Programmer tests should - Approach comparison

  • Minimize programmer waiting.
  • Run reliably.
  • Predict deployability.
  • Respond to behavior changes.
  • Not respond to structure changes.
  • Be cheap to write.
  • Be cheap to read.
  • Be cheap to change.

More general advice

  • Test both black box (purely on specs) and white box (look at the implementation and let that inform the tests you write). White-box-testing can improve test coverage, by better testing edge cases. 
  • Mindset: "This is so important, that we have to build an automated test suite for it. I don’t trust human testers to do the job."
  • Automated tests minimize faulty assumptions. They're a safety net for refactors later when the programmer doesn't know about all the previous assumptions and verbally communicated requirements.
  • Store related things together. So keep tests alongside the system under test.

"Write tests. Not too many. Mostly integration." - Guillermo Rauch

Automated Testing

Cypress vs Playwright differences

  • type -> fill
  • cy -> page
  • Add await
  • get -> locator
  • it -> test
  • cy.viewport ->
  • cy.scrollIntoView -> Playwright does automatically
  • Testing lib queries have shorter names on Playwright, and getBy* doesn’t assert by default.
  • (should.not.exist) -> tohavecount(0)
  • In Playwright, search in section by chaining off the parent’s locator.
  • Playwright it’s easy to forget to await.
  • describe vs test.describe

General Structure and Practices

React libraries

Testing Library Tips

Cypress Tips

More broadly, don't use the UI to build up state for each test. Instead, set it.

For example, don't actually run real login for each step. Instead, simulate it:
image

  it.only("should find 630 users when filtered by role of Salesperson or None", () => {
    cy.getByLabelText("Filter by Role")
      .click()
      .then(subject => {
        cy.getByLabelText("Salesperson", { container: subject }).click();
        cy.getByLabelText("None", { container: subject }).click();
      });
    cy.getByText("630 users");
  });

Instructions for downloading Cypress if corp network is blocking:

  1. Go to: https://download.cypress.io/desktop/4.2.0?platform=win32&arch=x64
    to download the cypress package.
  2. Copy the downloaded zip file to c:\temp
  3. Run the command (In a non powershell command line window):

set DEBUG=cypress:cli && set CYPRESS_INSTALL_BINARY=C:/temp/cypress.zip && npm install cypress
in the VSCode terminal or a cmd window

  1. Get the last cypress package run the command:
    npm i @testing-library/cypress

Cypress issues

Cypress isn't perfect.

Testing Different File Types

In browser testing / Integration Testing

Contract Testing (aka Consumer Driven Contract Testing)

Overview: https://www.linkedin.com/pulse/api-contract-testing-visual-guide-peter-thomas/
image

Test the client/server contract. Assure the API provides the expected responses, via tests. Here's why it's useful.

Tools:

Benefits:

  1. Can reveal unused interfaces - throws an error when there's unused provider code. If no apps are consuming it, obviously the code can be deleted from the provider (the API).

image

Visual Testing

Performance Testing / Monitoring

Property Based Testing

Generate valid random values and feed to tests. e.g. Pass random strings and ints to a func that accepts a string and int.

Validation Testing

https://ealush.com/vest

Mutation Testing

Change code and if it doesn't fail, that means you have holes in your test coverage.

Fuzz Testing / Randomized Testing / Test Generation (like Pex for C#, or QuickCheck which popularized the idea)

Recommended Reading

Testing React components with Jest and Enzyme
Node and JS Testing Best Practices

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant