Skip to main content

Fast Intro software testing

  • Unit Testing: Test each piece individually (like checking each piece of a puzzle).
  • Integration Testing: Make sure the pieces work well together.
  • E2E (End-to-End) testing: Test the final product to see if it meets expectations (like looking at the complete puzzle).

Unit Testing

Tools: Jest

Installation

npm install --save-dev jest

Example with Jest

Suppose you have a simple add function in add.js

function add(a, b) {
return a + b;
}
module.exports = add;

Create a test file add.test.js

const add = require('./add');

test('adds 1 + 2 to equal 3', () => {
expect(add(1, 2)).toBe(3);
});

Run Tests

Add a script in your package.json

"scripts": {
"test": "jest"
}

Then run with:

npm test

Automate the tests at each commit to catch regressions early.

Integration Test (tests the interactions between the different parts of the application)

E2E test (simulates a complete real user experience)

Tools: Cypress

Installation

npm install cypress --save-dev

Example with Cypress

Create a test file add.test.js

describe('Login Test', () => {
it('Logs in successfully', () => {

cy.visit('http://localhost:3000/login');

cy.get('input[name="username"]').type('user_test');
cy.get('input[name="password"]').type('password123');

cy.get('button[type="submit"]').click();

cy.url().should('include', '/home');
cy.contains('Bienvenue, user_test');
});
});

What we are testing here:

  • Opens the login page.
  • Populates form fields with test IDs.
  • Submits the form.
  • Checks if the URL has changed to the home page and if a welcome message is displayed.

Run Tests

npx cypress open