Challenge: Unit Test Using Test After Development
Apply what you have learned about making tests more effective by completing a test after development exercise.
We'll cover the following
Introduction
In this project, you’ll write a unit test that tests the behavior of a class using the test after development approach. An assessment rubric is provided to give you an indication of whether the principles taught in this section have been applied.
Self-assessment rubric
I have… | Emergent | Satisfactory | Excellent |
---|---|---|---|
Used parameterized tests | Did not use parameterized tests | Partially used parameterized tests | Used parameterized tests for test methods, wherever appropriate |
Used a setup method | No setup method used | N/A | Setup method used |
Pitched the tests at the right level | Inconsistent test approach—a combination of high-level and detailed approaches used | N/A | Consistent test approach—either a high-level or detailed approach adopted |
Quick dive into probability before unit testing
The challenge requires us to write code that calculates factorials, permutations, and combinations. Below is a brief description of each.
Factorial
The following is an outline of the factorial operation.
Calculation and example
is the product of all positive integers less than or equal to ,
For example:
Meaning of factorial
This operation is used in probability calculations where we want to find out the number of different ways to arrange distinct objects into a sequence. For instance, if we have three books on a bookshelf and we want to count the number of ways to arrange these books, we would calculate .
What is Permutation?
The following is an outline of the permutation operation.
Calculation and example
is a permutation that is calculated using factorials as follows:
For example:
Meaning of permutation
This operation is used in probability calculations where we want to find out the number of possible arrangements in a set when the order matters.
For instance, if we have eight people and we would like to pick a gold, silver, and bronze medal winner, the calculation to use would then be as follows:
Combination
The following is an outline of the combination operation.
Notation, calculations, and example
is a combination that is calculated using factorials as follows:
For example:
What is a combination?
This operation is used in probability calculations where we want to find out the number of possible arrangements in a set when the order does not matter.
For instance, if we have eight people and we would like to pick three people, the calculation to use would then be as follows:
Project requirement
Write the application code that contains a calculator class, which has three public methods:
- The first method calculates the factorial of a number.
- The second method calculates the permutation of two numbers.
- The third method calculates the combination of two numbers.
Ensure that all exceptions are taken into account when designing the class:
- Factorials can only be taken on non-negative numbers.
- For permutations and combinations, must be equal to, or larger than, .
- For permutations and combinations, and must be non-negative.
After completing the application code, write the test code that tests these three operations. Remember to ensure that the test code not only tests normal operation but exceptions as well.
Get hands-on with 1200+ tech skills courses.