Testing - CSU677 - Shoolini U

Testing

1. Introduction to Software Testing

Software testing is a critical process in software development that ensures the quality, functionality, and performance of applications. Testing helps identify and fix bugs, validate that the software meets the requirements, and ensure that it performs well under different conditions. There are various types of testing, each targeting different aspects of the software. This documentation will focus on Unit Testing, End-to-End Testing, and Performance Testing, along with the tools and frameworks commonly used for each.

2. Unit Testing

Unit Testing involves testing individual components or units of code, typically functions or methods, in isolation from the rest of the application. The goal is to verify that each unit performs as expected. Unit tests are often automated and run frequently to catch errors early in the development process.

2.1 Tools for Unit Testing

Several tools and frameworks are available for writing and running unit tests. Two popular ones are Jest and Mocha.

2.1.1 Jest

Jest is a comprehensive JavaScript testing framework developed by Facebook, designed to work with React applications. It provides a simple and powerful API, built-in mocking, and an easy setup for writing unit tests.

Example of a Unit Test Using Jest

// sum.js
function sum(a, b) {
    return a + b;
}
module.exports = sum;

// sum.test.js
const sum = require('./sum');

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

This example defines a simple function sum and tests it using Jest to verify that it correctly adds two numbers.

2.1.2 Mocha

Mocha is a flexible JavaScript test framework that runs on Node.js. It provides a rich set of features for writing asynchronous tests, and it integrates well with other tools like Chai for assertions.

Example of a Unit Test Using Mocha

// sum.js
function sum(a, b) {
    return a + b;
}
module.exports = sum;

// test/sum.test.js
const assert = require('assert');
const sum = require('../sum');

describe('Sum Function', () => {
    it('should return 3 when adding 1 and 2', () => {
        assert.strictEqual(sum(1, 2), 3);
    });
});

This example uses Mocha and Node's built-in assert module to test the sum function.

2.2 Best Practices for Unit Testing

3. End-to-End Testing

End-to-End (E2E) Testing involves testing the entire application flow from start to finish, simulating real user scenarios. E2E tests validate that all components of the application work together correctly, ensuring that the software meets user expectations.

3.1 Tools for End-to-End Testing

Several frameworks are available for performing E2E testing. Two widely-used ones are Cypress and Selenium.

3.1.1 Cypress

Cypress is a modern E2E testing framework that runs directly in the browser. It provides a simple API, automatic waiting, and powerful debugging capabilities, making it a popular choice for testing modern web applications.

Example of an End-to-End Test Using Cypress

// cypress/integration/sample_spec.js
describe('My First Test', () => {
    it('visits the app and checks the title', () => {
        cy.visit('https://example.com');
        cy.title().should('include', 'Example Domain');
    });
});

This example tests whether the title of the page at https://example.com includes the text "Example Domain."

3.1.2 Selenium

Selenium is a powerful and widely-used framework for automating web browsers. It supports multiple programming languages and can be used to write cross-browser tests. Selenium WebDriver is the core component that interacts with the browser.

Example of an End-to-End Test Using Selenium (JavaScript)

const { Builder, By, until } = require('selenium-webdriver');

(async function example() {
    let driver = await new Builder().forBrowser('firefox').build();
    try {
        await driver.get('https://example.com');
        let title = await driver.getTitle();
        console.log(title);
    } finally {
        await driver.quit();
    }
})();

This example uses Selenium WebDriver to open Firefox, navigate to https://example.com, retrieve the page title, and then close the browser.

3.2 Best Practices for End-to-End Testing

4. Performance Testing

Performance Testing evaluates how an application performs under various conditions, such as high traffic, heavy data loads, and extended operation. It helps identify bottlenecks, ensure that the application meets performance requirements, and improve the overall user experience.

4.1 Tools for Performance Testing

There are several tools available for performance testing, each with different strengths. Lighthouse and JMeter are two popular options.

4.1.1 Lighthouse

Lighthouse is an open-source tool from Google for auditing the performance of web pages. It provides insights into various aspects such as load times, accessibility, SEO, and best practices, and generates a detailed report with actionable recommendations.

Example of Running a Lighthouse Test

# Install Lighthouse globally
npm install -g lighthouse

# Run Lighthouse on a URL
lighthouse https://example.com --output html --output-path report.html

This command runs a Lighthouse audit on https://example.com and generates an HTML report.

4.1.2 JMeter

Apache JMeter is a powerful open-source tool for load testing and measuring the performance of web applications. It allows users to simulate a large number of users interacting with the application to assess its behavior under stress.

Example of a Simple JMeter Test Plan

<testPlan>
	<threadGroup>
        <numThreads>10</numThreads>
        <rampTime>60</rampTime>
        <loopCount>5</loopCount>
        <httpRequest>
            <url>https://example.com</url>
            <method>GET</method>
        </httpRequest>
	</threadGroup>
</testPlan>

This JMeter test plan simulates 10 users sending GET requests to https://example.com over a period of 60 seconds, looping 5 times.

4.2 Best Practices for Performance Testing