Software testing
Software testing is the process of evaluating and
verifying that a software product or application
does what it’s supposed to do.
Vaak een aparte rol in software-teams:
Los daarvan is testen een groot onderdeel van het werk van elke developer.
Software testing
Dit zijn ook voorbeelden van non-functional testing
omdat ze geen betrekking hebben op de functionele requirements.
Software testing
A unit test is a block of code that verifies the accuracy of a smaller, isolated block of application code.
describe('multiply', () => {
it('should return the two numbers multiplied by each other', () => {
// Arrange
const number1 = 5;
const number2 = 10;
// Act
const multiplication = multiply(number1, number2);
// Assert
expect(multiplication).toBe(50);
});
});
describe('multiply', () => {
it('should return the two numbers multiplied by each other', () => {
// Arrange & Act gebeuren hier impliciet
expect(multiply(5, 10)).toBe(50);
});
});
describe('mathematics', () => {
describe('subtraction', () => {
it('should return the second number subtracted by the first', () => {
expect(subtract(10, 3)).toBe(7);
});
});
describe('multiply', () => {
test('returns the two numbers multiplied by each other', () => {
expect(multiply(5, 10)).toBe(50);
});
});
});
describe
gebruik je om tests te groeperen
it
should
test
in plaats van it
Software testing
Jest is a delightful JavaScript Testing Framework with a focus on simplicity.
Designed to ensure correctness of any JavaScript codebase. It allows you to write tests with an approachable, familiar and feature-rich API that gives you results quickly.
npm install jest --save-dev
test
scripttype": "module"
toe{
// ...
"scripts": {
// Windows
"test": "set NODE_OPTIONS=--experimental-vm-modules && npx jest",
// Mac or Linux -> Change 'set' to 'export'
},
"type": "module",
// ...
}
Tests schrijven: oefening 1
describe
en it
/ test
om test(s) op te stellenTests schrijven: oefening 2
describe
en it
/ test
om test(s) op te stellenVS Code extension: Jest
VS Code extension: Jest
describe
)it
/ test
)
Tests schrijven: oefening 3
passed
die een getal als argument heeft
Tests schrijven: oefening 4
olderThan
die 2 argumenten heeft:
name
(string)age
(number)minimumAge
(number)minimumAge
// Given following input
const persons = [
{name: 'John', age: 27},
{name: 'Jane', age: 31},
{name: 'Joe', age: 41},
{name: 'Janet', age: 19}
];
const result = olderThan(persons, 30);
// ['Jane', 'Joe']
Software testing
body
naar grijs$
: shorthand for document.querySelector
Oefening: wijzig de beschrijving van je eerste taak door in de console Javascript uit te voeren (tip: innerText
)Software testing
Software testing
Tests schrijven: oefening 5
Tests schrijven: oefening 5
function displayCategoryOption(categories, $categorySelect) {
$categorySelect.insertAdjacentHTML(
"beforeend",
mapCategoriesToOptionsHtml(categories)
// Replaced below with mapCategoriesToOptionsHtml to make it testable:
//
// categories
// .map((category) => {
// return `
// <option value=${category.id}>${category.name}</option>
// `;
// })
// .join("")
);
}
function mapCategoriesToOptionsHtml(categories) {
return categories
.map((category) => {
return `
<option value=${category.id}>${category.name}</option>
`;
})
.join("");
}
Tests schrijven: oefening 5
describe("mapCategoriesToOptionsHtml", () => {
it('should map an array of categories to corresponding <option> elements', () => {
const categories = [
{ id: 1, name: "Huishouden" },
{ id: 2, name: "Hobbies" },
{ id: 3, name: "School" }
];
const optionsAsHtml = mapCategoriesToOptionsHtml(categories);
expect(optionsAsHtml).toBe(`
<option value="1">Huishouden</option>
<option value="2">Hobbies</option>
<option value="3">School</option>
`);
});
});