Adding chapter 5 source code

This commit is contained in:
muassif
2021-02-21 21:40:34 +04:00
committed by GitHub
parent e8469ec365
commit e662762d09
24 changed files with 348 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
#empty file
+4
View File
@@ -0,0 +1,4 @@
# myadd.py with add two numbers
def add(x, y):
"""This function adds two numbers"""
return x + y
+8
View File
@@ -0,0 +1,8 @@
# myadd3.py is a class with with add two numbers method
def add( x, y):
"""This function adds two numbers"""
if (not isinstance(x, (int, float))) | \
(not isinstance(y, (int, float))):
raise TypeError("only numbers are allowed")
return x + y
+19
View File
@@ -0,0 +1,19 @@
# mycalc.py with add,subtract, multiply and divide functions
class MyCalc:
def add(self, x, y):
"""This function adds two numbers"""
return x + y
def subtract(self, x, y):
"""This function subtracts two numbers"""
return x - y
def multiply(self, x, y):
"""This function subtracts two numbers"""
return x * y
def divide(self, x, y):
"""This function subtracts two numbers"""
return x / y
+1
View File
@@ -0,0 +1 @@
#empty file
Binary file not shown.
+32
View File
@@ -0,0 +1,32 @@
# test_myadd3.py test suite for myadd function
import pytest
from mypytest.src.myadd3 import add
def test_add1():
""" test case to validate two positive numbers"""
assert add(10, 5) == 15
def test_add2():
""" test case to validate two positive numbers"""
assert add(10, -5) == 5, "should be 5"
def test_add3():
""" test case to validate two positive numbers"""
assert add(-10, 5) == -5
def test_add4():
""" test case to validate two positive numbers"""
assert add(-10, -5) == -15
def ttest_typeerror1():
""" test case to check if we can handle non number input"""
with pytest.raises(TypeError):
add('a', 5)
def ttest_typeerror2():
""" test case to check if we can handle non number input"""
with pytest.raises(TypeError, match="only numbers are allowed"):
add('a', 'b')
+24
View File
@@ -0,0 +1,24 @@
# test_myadd4.py test suite for myadd function using markers
import sys
import pytest
from mypytest.src.myadd3 import add
@pytest.mark.skip
def test_add1():
""" test case to validate two positive numbers"""
assert add(10, 5) == 15
@pytest.mark.skipif(sys.version_info > (3,6),
reason=" skipped for release > than Python 3.6")
def test_add2():
""" test case to validate two positive numbers"""
assert add(10, -5) == 5, "should be 5"
@pytest.mark.xfail(sys.platform == "darwin",
reason="ignore exception for mac")
def test_add3():
""" test case to validate two positive numbers"""
assert add(-10, 5) == -5
raise Exception()
+14
View File
@@ -0,0 +1,14 @@
# test_myadd5.py test suite using parameterize marker
import sys
import pytest
from mypytest.src.myadd3 import add
@pytest.mark.parametrize("x,y,ans",
[(10,5,15),(10,-5,5),
(-10,5,-5),(-10,-5,-15)],
ids=["pos-pos","pos-neg",
"neg-pos", "neg-neg"])
def test_add(x, y, ans):
""" test case to validate two positive numbers"""
assert add(x, y) == ans
+23
View File
@@ -0,0 +1,23 @@
# test_mycalc1.py test calc functions using test fixture
import sys
import pytest
from mypytest.src.myadd3 import add
from mypytest.src.mycalc import MyCalc
@pytest.fixture(scope="module")
def my_calc():
return MyCalc()
@pytest.fixture
def test_data():
return {'x':10, 'y':5}
def test_add(my_calc, test_data):
""" test case to validate two positive numbers"""
assert my_calc.add(test_data.get('x'), test_data.get('y')) == 15
def test_subtract(my_calc, test_data):
""" test case to validate two positive numbers"""
assert my_calc.subtract(test_data.get('x'), test_data.get('y')) == 5
+28
View File
@@ -0,0 +1,28 @@
# test_mycalc2.py test calc functions using test fixture
import pytest
from mypytest.src.myadd3 import add
from mypytest.src.mycalc import MyCalc
@pytest.fixture(scope="module")
def my_calc():
my_calc = MyCalc()
yield my_calc
del my_calc
@pytest.fixture
def data_set(request):
dict = {'x':10, 'y':5}
def delete_dict():
del dict
request.addfinalizer(delete_dict)
return dict
def test_add(my_calc, data_set):
""" test case to validate two positive numbers"""
assert my_calc.add(data_set.get('x'), data_set.get('y')) == 15
def test_subtract(my_calc, data_set):
""" test case to validate two positive numbers"""
assert my_calc.subtract(data_set.get('x'), data_set.get('y')) == 5
@@ -0,0 +1 @@
#empty file
+4
View File
@@ -0,0 +1,4 @@
# myadd.py with add two numbers
def add(x, y):
"""This function adds two numbers"""
return x + y
+8
View File
@@ -0,0 +1,8 @@
# myadd2.py is a class with with add two numbers method
class MyAdd:
def add(self, x, y):
"""This function adds two numbers"""
if (not isinstance(x, (int, float))) | \
(not isinstance(y, (int, float))) :
raise TypeError("only numbers are allowed")
return x + y
+19
View File
@@ -0,0 +1,19 @@
# mycalc.py with add,subtract, multiply and divide functions
class MyCalc:
def add(self, x, y):
"""This function adds two numbers"""
return x + y
def subtract(self, x, y):
"""This function subtracts two numbers"""
return x - y
def multiply(self, x, y):
"""This function subtracts two numbers"""
return x * y
def divide(self, x, y):
"""This function subtracts two numbers"""
return x / y
@@ -0,0 +1 @@
#empty file
@@ -0,0 +1,24 @@
#test_myadd1.py test suite for myadd function
import unittest
from myunittest.src.myadd.myadd import add
class MyAddTestSuite(unittest.TestCase):
def test_add1(self):
""" test case to validate two positive numbers"""
self.assertEqual(15, add(10 , 5), "should be 15")
def test_add2(self):
""" test case to validate positive and negative numbers"""
self.assertEqual(5, add(10 , -5), "should be 5")
def test_add3(self):
""" test case to validate positive and negative numbers"""
self.assertEqual(-5, add(-10 , 5), "should be -5")
def test_add4(self):
""" test case to validate two negative numbers"""
self.assertEqual(-15, add(-10 , -5), "should be -15")
if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,30 @@
#test_myadd2.py test suite for myadd2 class method
import unittest
from myunittest.src.myadd.myadd2 import MyAdd
class MyAddTestSuite(unittest.TestCase):
def setUp(self):
self.myadd = MyAdd()
def tearDown(self):
del (self.myadd)
def test_add1(self):
""" test case to validate two positive numbers"""
self.assertEqual(15, self.myadd.add(10 , 5), "should be 15")
def test_add2(self):
""" test case to validate positive and negative numbers"""
self.assertEqual(5, self.myadd.add(10 , -5), "should be 5")
def test_add3(self):
""" test case to validate positive and negative numbers"""
self.assertEqual(-5, self.myadd.add(-10 , 5), "should be -5")
def test_add4(self):
""" test case to validate two negative numbers"""
self.assertEqual(-15, self.myadd.add(-10 , -5), "should be -15")
if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,19 @@
#test_myadd3.py test suite for myadd2 class method to validate errors
import unittest
from myunittest.src.myadd.myadd2 import MyAdd
class MyAddTestSuite(unittest.TestCase):
def setUp(self):
self.myadd = MyAdd()
def test_typeerror1(self):
""" test case to check if we can handle non number input"""
self.assertRaises(TypeError, self.myadd.add, 'a' , -5)
def test_typeerror2(self):
""" test case to check if we can handle non number input"""
self.assertRaises(TypeError, self.myadd.add, 'a' , 'b')
if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,27 @@
# test_mycalc_add.py test suite for add class method
import unittest
from test_mycalc_add import MyCalcAddTestSuite
from test_mycalc_subtract import MyCalcSubtractTestSuite
from test_mycalc_multiply import MyCalcMultiplyTestSuite
from test_mycalc_divide import MyCalcDivideTestSuite
def run_mytests():
test_classes = [MyCalcAddTestSuite, MyCalcSubtractTestSuite,
MyCalcMultiplyTestSuite,MyCalcDivideTestSuite ]
loader = unittest.TestLoader()
test_suites = []
for t_class in test_classes:
suite = loader.loadTestsFromTestCase(t_class)
test_suites.append(suite)
final_suite = unittest.TestSuite(test_suites)
runner = unittest.TextTestRunner()
results = runner.run(final_suite)
#print(results)
if __name__ == '__main__':
run_mytests()
@@ -0,0 +1,16 @@
# test_mycalc_add.py test suite for add class method
import unittest
from myunittest.src.mycalc.mycalc import MyCalc
class MyCalcAddTestSuite(unittest.TestCase):
def setUp(self):
self.calc = MyCalc()
def test_add(self):
""" test case to validate two positive numbers"""
self.assertEqual(15, self.calc.add(10, 5), "should be 15")
if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,15 @@
#test_mycalc_divide.py test suite for divide class method
import unittest
from myunittest.src.mycalc.mycalc import MyCalc
class MyCalcDivideTestSuite(unittest.TestCase):
def setUp(self):
self.calc = MyCalc()
def test_divide(self):
""" test case to validate two positive numbers"""
self.assertEqual(2, self.calc.divide(10 , 5), "should be 2")
if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,15 @@
#test_mycalc_multiply.py test suite for multiply class method
import unittest
from myunittest.src.mycalc.mycalc import MyCalc
class MyCalcMultiplyTestSuite(unittest.TestCase):
def setUp(self):
self.calc = MyCalc()
def test_multiply(self):
""" test case to validate two positive numbers"""
self.assertEqual(50, self.calc.multiply(10 , 5), "should be 50")
if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,15 @@
#test_mycalc_subtract.py test suite for substract class method
import unittest
from myunittest.src.mycalc.mycalc import MyCalc
class MyCalcSubtractTestSuite(unittest.TestCase):
def setUp(self):
self.calc = MyCalc()
def test_subtract(self):
""" test case to validate two positive numbers"""
self.assertEqual(5, self.calc.subtract(10 , 5), "should be 5")
if __name__ == '__main__':
unittest.main()