At the end of this class you should be able to understand
- What testing is and why it is important
- How to test your software and make it fail
- Black box and white box testing
- How to write a reasonably minimal and sufficient amount of test cases
- What is test driven development and when to use it
- How to test your code and check coverage with Jest
- Basics of debugging and the bug lifecycle
In this course we use Jest for testing. Check it out at https://jestjs.io/
To get started, well... follow the instruction at the getting started page: https://jestjs.io/docs/en/getting-started.html
And specifically, run npm install --save-dev jest in your folder
Also ensure that in your package.json you have
"scripts":
{
"test": "jest"
}
Also add this to activate coverage into your package.json:
"jest": {
"verbose": true,
"collectCoverage": true
},
to see very very very simple examples of tests, lets start with these two files:
Reading material - required for exam
- We take selected chapters from the book on software testing, as described in this pdf document, accessible only with UNITN account. Specifically, here is what is required:
- Ch1: tutto
- Ch2, Ch3: come evidenziato
- Ch4: come evidenziato, incluso tutto il white-box testing
- Ch8: come evidenziato
- (The entire book, called "The art of software testing", can be found online (I have seen some people posted PDFs for it) - again you are not required to read it all, only the parts as described above)
- From jest, you are expected to know how to use jest to do write test cases, how to test asynch code, and how to perform setup and teardown. You are not expected to learn all matchers, but you should have a general idea of what they are and which kinds of matchers are avail. Basically, you should be familiar with all the "introduction" items (getting started - up to "Running from command line" excluded, using matchers, setup and teardown) - except testing asynch code and mock functions.
- you should also know how to test for coverage with Jest
Additional readings and material
Practicing with software testing and debugging
invert
Function: k_invert(a, k)
Given an array a of positive integer numbers, take the last k items and put them on top
For example, array k_invert([1,2,3,4,5],2) returns [4,5,1,2,3]
Returns null if a is not an array of positive integers, if k is not a positive integer, and if k is greater than the length of a
- Write black box test cases (in jest, adding equivalence partitions as comments in the file k_invert.test.js)
- Write function, in file k_invert.js
- check coverage and consider writeing white box test cases to improve coverage if needed
Coverage
For the below,
- Write the function,
- write test cases for it, based on black box testing and equivalence partitioning and boundary analysis
- write test cases to achieve 100% coverage
- Function that computes the square root of a positive number
- Function that receives four positive integers and returns if these integers can be the length of the sides of a rectangle.
- Function that receives three positive integers and returns if these integers can be the length of the sides of a triangle, and also returns which kind of a triangle this is.
Given this function, achieve
- E1: 100% statement coverage
- E2: 100% branch coverage