Quick Design Unit Testing
If you want to add unit tests to your calculation functions in Quick Design then this can be easily done with Jest. Jest is simple to set up and run.
To do this in Quick Design it is best to abstract your calculation functions into a separate js file in your utils folder of your calc pack. Even if you don't want to unit test this is a good idea as it promotes better organization of your code which can improve:
- Modularity ๐
- Reusability ๐๐
- Readability ๐๐๐
- Maintainability ๐๐๐๐
You can find a sample of how to write unit tests in ./unit_tests/eng_calcs/test.js
This file tests utils/eng_calcs.js
#
Setting up Unit Tests#
Step 1: Install Jestnpm install --save-dev jest
#
Setup NPM ScriptAdd this to your package.json scripts section
If you already have a scripts section just add:
"test": "jest"
#
Step 2: Write your testsStart writing your unit tests.
You can have as many files to do this as you want.
These files should all end in the extension .test.js
Import your calculations file into the relevant .test.js
file
const engCalcs = require('../utils/eng_calcs');
Generally each test is an assertion that a given input should equal a given output, i.e.
There are different ways to test than using toBe, you can find them by reading the docs here;
Grouping Tests
You can group tests with the describe function.
#
Step 3: Run your testsRun this in your terminal, if everything is set up correctly it should find and run your tests.
#
Writing Quality Tests1) Check expected behavior (i.e. we know 1 + 2 should always equal 3). 2) Check more complex behaviors (i.e. we want our module to handle negative numbers so we test that 1 + -3 = -2). 3) Test boundary conditions (i.e We know we cannot handle division by 0 so what should 1 / 0 return?).
Other things to check?
- Large numbers
- Input type validation
At the end of the day, unit tests are only useful if your underlying assumptions and expected results are correct. Passing unit tests doesn't mean that your calculations are correct and valid from an engineering standpoint if you have got the formula, check, or process wrong from the start. Unit tests are here to help you ensure you cover boundary cases, encourage better code modularity, and check edge cases.