forked from purcellconsult/Cracking-Python-Bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
06_unit_test_module.py
108 lines (90 loc) · 3.18 KB
/
06_unit_test_module.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
########################################
# Unit testing in python
# ----------------------
# Learn the various ways to unit test
# in python. Accompanying Google Doc:
# https://docs.google.com/presentation/d/1K8SN0TzK52700sM5QAhQii_a7KbutCwRA0wJQY8l6Z4/edit?usp=sharing
#
# By Doug Purcell
# http://www.purcellconsult.com
#
###########################################
# Software testing is a profession in itself!
# UNITTEST
# PYTEST
# NOSE
# DOCTEST
# PYLINT
# HYPOTHESIS
# UNIT TEST MODULE
# ----------------
# A unit testing framework inspired by JUnit
# Built into the python core
# Supports test automation, sharing of setup and shutdown of code tests
# Aggregation of tests into collections and independence of tests from reporting framework
# UNIT TEST DEMO
# --------------
import unittest
class TestListMethods(unittest.TestCase):
"""
Python list methods:
https://docs.python.org/3/tutorial/datastructures.html
"""
def test_append(self):
x = [1, 2, 3]
x.append(10)
self.assertTrue(x, [1, 2, 3, 10])
def test_count(self):
cars = ['Mercedes', 'Lexus', 'Ford', 'BMW', 'Ferrari', 'Honda', 'Toyota', 'Ford']
self.assertEqual(cars.count('Ford'), 2)
def test_pop(self):
fruits = ['apple', 'orange', 'tangerine', 'pineapple']
self.assertEqual(fruits.pop(), 'pineapple')
def test_reverse(self):
music = ['jazz', 'r and b', 'rock and roll', 'edm']
self.assertNotEqual(music.reverse(), music)
def test_is_not(self):
x = []
y = [1]
self.assertIsNot(x, y)
def test_count_equal(self):
a = [2, 4, 6]
b = [6, 2, 4]
self.assertCountEqual(a, b)
# Running tests via command line
# ------------------------------
# The unittest module can be used from the command line
# to run tests from modules, classes, or individual methods.
# python -m unittest test_module1 test_module2
# python -m unittest test_module.TestClass
# python -m unittest test_module.TestClass.test_method
# Common assert methods in unittest
# ---------------------------------
# Learn about them here: https://docs.python.org/3/library/unittest.html#unittest.TestCase.debug
# assertEqual(a, b)
# assertNotEqual(a, b)
# assertTrue(x)
# assertFalse(x)
# assertIs(a, b)
# assertIsNot(a, b)
# assertIsNone(x)
# assertIsNotNone(x)
# assertIn(a, b)
# assertNotIn(a, b)
# assertIsInstance(a, b)
# assertNotIsInstance(a, b)
# Pytest module
# --------------
# pytest is a framework that makes it easy to write small tests.
# Compared to unittest it has detailed info on failing assert statements.
# * No need to remember self.assert *
# Autodiscovery of test modules and functions
# Can run unittest and nose test suites out of the box
# Strong plugin architecture. Has 315+ external plugins.
# Installing pytest
# ------------------
# in the command line run the following:
# $ pip3 install -U pytest
# Test that you've installed the correct version
# $ pytest --version
# In PyCharm - File -> Settings -> Tools -> Python Integration Tools -> Default test runner -> pytest