forked from purcellconsult/Cracking-Python-Bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
06_py_test_demo.py
58 lines (38 loc) · 1.09 KB
/
06_py_test_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import pytest
# Pytest Demo
# -----------
def add_nums(x, y):
return x + y
def test_add_nums():
assert add_nums(5, 15) == 20
# Output when ran:
# -----------------
# platform linux -- Python 3.6.7, pytest-5.0.0, py-1.8.0, pluggy-0.12.0
# rootdir: /home/doug/PycharmProjects/master_python_coursecollected 1 item
#
# 6_py_test_demo.py . [100%]
# uses raises helper to assert that some code raises an exception
def x():
raise SyntaxError(1)
def test_x():
with pytest.raises(SyntaxError):
x()
# Group multiple tests in a class
# -------------------------------
# More examples of the assert statement in pytest
class TestClass(object):
def test_one(self):
greet = 'Hello World!'
assert '!' in greet
def test_two(self):
x, y = 5, 10
assert x < y
def test_three(self):
x, y, z = 5, 10, 15
assert (x * y) != z
def test_four(self):
statement = True
assert statement is True
def test_five(self):
money = []
assert money is not None