Skip to content

Instantly share code, notes, and snippets.

View AndyLPK247's full-sized avatar

Pandy Knight AndyLPK247

View GitHub Profile
@AndyLPK247
AndyLPK247 / calc.py
Last active March 10, 2017 02:30
Python Testing 101 unittest Example: Calculator Class
class Calculator(object):
def __init__(self):
self._last_answer = 0.0
@property
def last_answer(self):
return self._last_answer
def add(self, a, b):
self._last_answer = a + b
@AndyLPK247
AndyLPK247 / test_calc.py
Created March 10, 2017 02:37
Python Testing 101 unittest Example: CalculatorTest Class
from com.automationpanda.example.calc import Calculator
NUMBER_1 = 3.0
NUMBER_2 = 2.0
FAILURE = 'incorrect value'
class CalculatorTest(unittest.TestCase):
def setUp(self):
@AndyLPK247
AndyLPK247 / test_calc.py
Created March 10, 2017 02:53
Python Testing 101 unittest Example: XML Report Generator
if __name__ == '__main__':
import xmlrunner
unittest.main(
testRunner=xmlrunner.XMLTestRunner(output='test-reports'),
failfast=False,
buffer=False,
catchbreak=False)
@AndyLPK247
AndyLPK247 / calc_func.py
Created March 11, 2017 04:57
Python Testing 101 doctest Example: Doctests in Docstrings
def add(a, b):
"""
Adds two numbers.
>>> add(3, 2)
5
"""
return a + b
@AndyLPK247
AndyLPK247 / calc_class.py
Created March 11, 2017 05:04
Python Testing 101 doctest Example: Calculator Class
class Calculator(object):
def __init__(self):
self._last_answer = 0.0
@property
def last_answer(self):
return self._last_answer
def add(self, a, b):
self._last_answer = a + b
@AndyLPK247
AndyLPK247 / test_calc_class.txt
Created March 11, 2017 05:08
Python Testing 101 doctest Example: Calculator Doctests
The ``com.automationpanda.example.calc_class`` module
=====================================================
>>> from com.automationpanda.example.calc_class import Calculator
>>> calc = Calculator()
>>> calc.add(3, 2)
5
@AndyLPK247
AndyLPK247 / pytest.ini
Created March 14, 2017 02:55
Python Testing 101 pytest Example: Config File
# Add pytest options here
[pytest]
@AndyLPK247
AndyLPK247 / calc_func.py
Created March 14, 2017 02:58
Python Testing 101 pytest Example: Math Functions
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
@AndyLPK247
AndyLPK247 / test_calc_func.py
Created March 14, 2017 03:08
Python Testing 101 pytest Example: Math Function Tests (2)
def test_divide_by_zero():
with pytest.raises(ZeroDivisionError) as e:
divide(NUMBER_1, 0)
assert "division by zero" in str(e.value)
@AndyLPK247
AndyLPK247 / test_calc_func.py
Created March 14, 2017 03:09
Python Testing 101 pytest Examples: Math Function Tests (3)
@pytest.mark.parametrize("a,b,expected", [
(NUMBER_1, NUMBER_2, NUMBER_1),
(NUMBER_2, NUMBER_1, NUMBER_1),
(NUMBER_1, NUMBER_1, NUMBER_1),
])
def test_maximum(a, b, expected):
assert maximum(a, b) == expected
@pytest.mark.parametrize("a,b,expected", [