标签:one 方法 判断 init cal 断言 ret 继承 课堂
import unittest
def add(a, b):
return a + b
def subtract(a, c):
return a - b
class MyTest(unittest.TestCase):
def test_add(self):
self.assertEqual(8, add(5, 4)) #断言是否相等
def test_subtract(self):
self.assertEqual(2, subtract(5, 3))
if __name__ == '__main__':
unittest.main()
import unittest
person = {'name':'Mike', 'age': 20}
numbers = [1, 2, 3, 88, 7, 44]
s = '优品课堂 codeclassroom.com'
class TestAssert(unittest.TestCase):
def test_assert_method(self):
#self.assertEqual('Mike', person.get('name'))
#self.assertTrue("优品课堂" in s)
#self.assertIn("优品课堂", s)
#self.assertAlmostEqual(3.3,1.1+2.2)
#self.assertIs(True + 1, 2)
#self.assertIsNone(person.get('Name', None))
#self.assertIsInstance(numbers[0], int)
#self.assertGreater(7, numbers[0])
if __name__ == '__main__':
unittest.main()
class Calculator:
def __init__(self, x, y):
self.x = x
self.y = y
def add(self):
return self.x + self.y
def subtract(self):
return self.x - self.y
if __name__ == '__main__':
c = Calculator(5, 3)
print(c.add())
import unittest
from calculator import Calculator
class CalculatorTest(unittest.TestCase):
def setUp(self):
self.c = Calculator(5, 3)
def test_add(self):
# c = Calculator(5, 3)
self.assertEqual(8, self.c.add())
def test_subtract(self):
# c =Calculator(8, 4)
self.assertEqual(4, self.c.subtract())
def tearDown(self):
del self.c
if __name__ == "__main__":
unittest.main()
标签:one 方法 判断 init cal 断言 ret 继承 课堂
原文地址:https://www.cnblogs.com/linyk/p/11482167.html