Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fc34f25

Browse files
authored
Add example of using custom assertions (#6)
1 parent e5a0195 commit fc34f25

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__pycache__/

README.md

+40-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,52 @@
11
# Codewars Test Framework for Python
22

3-
4-
## Example
3+
### Basic Example
54

65
```python
7-
from solution import add
86
import codewars_test as test
7+
from solution import add
98

109
@test.describe('Example Tests')
1110
def example_tests():
11+
1212
@test.it('Example Test Case')
1313
def example_test_case():
1414
test.assert_equals(add(1, 1), 2, 'Optional Message on Failure')
1515
```
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

Comments
 (0)