unittest Testing Framework
Qualified supports the
unittest unit testing framework.
import solutionfrom the
solutionmodule and optional preloaded code can be accessed using
Here is a minimal example of unit testing in Qualified's Python 3.7 environment by explicitly importing the
preloaded modules. For project code challenges, these imports can be any module name in the workspace.
import unittest import preloaded # preloaded code you can provide the solution and/or test module (omit if preloaded is empty) import solution # the candidate's solution class Test(unittest.TestCase): def test_add(self): ''' add two numbers ''' self.assertEqual(solution.add(2, 1), 3) def test_subtract(self): ''' subtract two numbers ''' self.assertEqual(solution.subtract(2, 1), 1) def test_division_by_zero(self): ''' divide by zero ''' self.assertRaises(ZeroDivisionError, solution.divide, 42, 0)
As demonstrated above, methods intended to be tested must begin with the prefix
Test class name is flexible. Verbose test descriptions can be provided with a simple docstring.
If you're using Python versions older than 3.7 in our code runner, the solution and preloaded modules are implicitly imported into the testing module as
from solution import *.
Visit unittest — Basic Example for a simple, platform-agnostic example of
The crux of each test is a call to
assertEqual() to check for an expected result;
assertFalse() to verify a condition; or
assertRaises() to verify that a specific exception gets raised. These methods are used instead of the
assert statement so the test runner can accumulate all test results and produce a report.