|
1 | 1 | # Codewars Test Framework for Python
|
2 | 2 |
|
3 |
| - |
4 |
| -## Example |
| 3 | +### Basic Example |
5 | 4 |
|
6 | 5 | ```python
|
7 |
| -from solution import add |
8 | 6 | import codewars_test as test
|
| 7 | +from solution import add |
9 | 8 |
|
10 | 9 | @test.describe('Example Tests')
|
11 | 10 | def example_tests():
|
| 11 | + |
12 | 12 | @test.it('Example Test Case')
|
13 | 13 | def example_test_case():
|
14 | 14 | test.assert_equals(add(1, 1), 2, 'Optional Message on Failure')
|
15 | 15 | ```
|
| 16 | + |
| 17 | +### Using Other Assertions |
| 18 | + |
| 19 | +Any function that raises an `AssertionError` can be used instead of `codewars_test` assertions: |
| 20 | + |
| 21 | +```python |
| 22 | +import numpy as np |
| 23 | +import pandas as pd |
| 24 | +import codewars_test as test |
| 25 | + |
| 26 | +@test.describe('Example Tests') |
| 27 | +def test_custom_assertions(): |
| 28 | + |
| 29 | + @test.it('Test something in numpy') |
| 30 | + def test_numpy_assertion(): |
| 31 | + actual = np.reshape(range(16), [4, 4]) |
| 32 | + expected = np.reshape(range(16, 0, -1), [4, 4]) |
| 33 | + np.testing.assert_equal(expected, actual) |
| 34 | + |
| 35 | + @test.it('Test something in pandas') |
| 36 | + def test_pandas_assertion(): |
| 37 | + actual = pd.DataFrame({'foo': [1, 2, 3]}) |
| 38 | + expected = pd.DataFrame({'foo': [1, 42, 3]}) |
| 39 | + pd.testing.assert_frame_equal(expected, actual) |
| 40 | + |
| 41 | + @test.it('Test something using a custom assertion') |
| 42 | + def test_custom_assertion(): |
| 43 | + def custom_assert_eq(actual, expected, msg=None): |
| 44 | + if actual != expected: |
| 45 | + default_msg = f'`{actual}` did not equal expected `{expected}`' |
| 46 | + raise AssertionError(default_msg if msg is None else msg) |
| 47 | + |
| 48 | + actual = 2 |
| 49 | + expected = 1 |
| 50 | + custom_assert_eq(actual, expected) |
| 51 | +``` |
| 52 | + |
0 commit comments