As a widely used programming language, JavaScript plays a very important role in the Internet field. In the process of software development, testing is an indispensable part. As projects become larger and code becomes more complex, manual testing becomes more time-consuming and error-prone. Therefore, unit testing and automated testing receive more and more attention. This article will introduce readers to unit testing and automated testing in JavaScript, with specific code examples.
1. Unit Testing
Unit testing refers to the process of checking and verifying the smallest testable unit in the software. In JavaScript, the smallest testable unit can be a function, method, class, etc., or it can be a small piece of code. The purpose of unit testing is to check the correctness and stability of the code and find potential problems in the code.
Let’s take the Jest framework as an example to introduce unit testing in JavaScript.
First, we need to install Jest in the project. You can use the npm command to install it. The command is as follows:
npm install jest --save-dev
Among them, --save-dev will add Jest to the development dependencies.
Next, we need to write test cases. Suppose we have the following addition module:
function add(a, b) {
return a b;
}
We need to write test cases to test the correctness of this module. In the project root directory, we create a file called add.test.js with the following code:
const add = require('./add');
test('adds 1 2 to equal 3', () => {
expect(add(1, 2)).toBe(3);
});
test('adds -1 2 to equal 1', () => {
expect(add(-1, 2)).toBe(1);
});
test('adds 0.1 0.2 to equal 0.3 ', () => {
expect(add(0.1, 0.2)).toBeCloseTo(0.3);
});
In the test case, we use the test() statement to define The name of the test case and the test function. In the test function, we use expect() and toBe() statements to verify the correctness of the code. The toBe() statement is used to compare equality, and the toBeCloseTo() statement is used to compare proximity. The command to run the test case is as follows:
npm test
The running result is as shown in the figure below:
The test case runs successfully and our code can run correctly. This way, we can release code with confidence!
2. Automated testing
Automated testing refers to the process of testing software using scripts, tools, etc. Compared with manual testing, automated testing has the advantages of speed, accuracy, and repeatability, and can greatly improve the efficiency and quality of testing. In JavaScript, automated testing is also very important.
Let’s take Selenium and ChromeDriver as examples to introduce automated testing in JavaScript.
First, we need to install Selenium and ChromeDriver in the project. You can use the npm command to install it. The command is as follows:
npm install selenium-webdriver chromedriver --save-dev
Among them, selenium-webdriver is the JavaScript implementation of Selenium, and chromedriver is the JavaScript implementation of ChromeDriver. .
Next, we need to write a test script. Suppose we have the following login module:
function login(username, password) {
if (username === 'admin' && password === 'admin') {
return true;
} else {
return false;
}
}
We need to write a test script to test the correctness of this module. In the project root directory, we create a file called login.spec.js with the following code:
const { Builder, By, Key, until } = require('selenium-webdriver');
(async function loginTest() {
let driver = await new Builder().forBrowser('chrome').build();
try {
await driver.get('http://example.com/login'); await driver.findElement(By.name('username')).sendKeys('admin', Key.TAB); await driver.findElement(By.name('password')).sendKeys('admin', Key.RETURN); await driver.wait(until.titleIs('My Dashboard'), 1000); console.log('Test passed!');
} finally {
await driver.quit();
}
})();
In the test script, we use Selenium and ChromeDriver for automated testing. We first create a browser instance, then open the login page, enter the user name and password, jump to the user interface, and finally output the test pass information. The command to run the test script is as follows:
node login.spec.js
The running result is as shown below:
The test script runs successfully and our code can run correctly . This way, we can release the code with confidence!
Summary
This article introduces unit testing and automated testing in JavaScript. Unit testing refers to the inspection and verification of the smallest testable unit, and the Jest framework is usually used to write test cases and perform testing; automated testing refers to testing software using scripts, tools, etc., and Selenium and ChromeDriver are usually used to write test scripts and conduct testing. I hope this article can help readers understand JavaScript testing.
The above is the detailed content of Learn about unit testing and automated testing in JavaScript. For more information, please follow other related articles on the PHP Chinese website!