EE308 Lab11

sunshine1211111 2021-12-16 15:06:23

The Link Your Class

https://bbs.csdn.net/forums/MUEE308FZ

The Link of Requirement of This Assignment

 https://bbs.csdn.net/topics/603748107

The Aim of This Assignment

Software Testing

MU STU ID and FZU STU ID

19105274-831901203

 


 

For today’s experiment , I chose the topic which is related to python. 

 1. unittest assert test

  1. import unittest, random
  2.  
  3. # Test Class
  4. class MyClass(object):
  5.  
  6.     @classmethod
  7.     def sum(self, a, b):
  8.         return a + b
  9.  
  10.     @classmethod
  11.     def div(self, a, b):
  12.         return a / b
  13.  
  14.     @classmethod
  15.     def retrun_None(self):
  16.         return None
  17.  
  18.  
  19. # Unit Test Class
  20. class MyTest(unittest.TestCase):
  21.     # assertEqual()
  22.     def test_assertEqual(self):
  23.         # test if a+b equals sum or not
  24.         try:
  25.             a, b = 1, 2
  26.             sum = 3
  27.             self.assertEqual(a + b, sum, 'assert failed!%s + %s != %s' % (a, b, sum))
  28.         except AssertionError as e:
  29.             print(e)
  30.  
  31.     # assertNotEqual()
  32.     def test_assertNotEqual(self):
  33.         # fix missing three lines of codes below ‘try’, test if b-a equals res or not
  34.         try:
  35.             a, b = 1, 2
  36.             res = 0
  37.             self.assertNotEqual((b - a), (res), 'assert failed!%s - %s != %s' % (b, a, res))
  38.         except AssertionError as e:
  39.             print(e)
  40.  
  41.     # assertTrue()
  42.     def test_assertTrue(self):
  43.         try:
  44.             self.assertTrue(1 == 1, "False expression")
  45.         except AssertionError as e:
  46.             print(e)
  47.  
  48.     # assertFalse()
  49.     def test_assertFalse(self):
  50.         # fix missing codes below ‘try’, only a line of codes needed
  51.         try:
  52.             self.assertFalse(1 != 1, "False expression")
  53.         except AssertionError as e:
  54.             print(e)
  55.  
  56.     # assertIs()
  57.     def test_assertIs(self):
  58.         # test a and b are totally same
  59.         try:
  60.             a = 12
  61.             b = a
  62.             self.assertIs(a, b, "%s and %s are not same" % (a, b))
  63.         except AssertionError as e:
  64.             print(e)
  65.  
  66.     # assertIsInstance()
  67.     def test_assertIsInstance(self):
  68.         # fix missing codes below ‘y=object’ to test type(x) != y, only a line of codes needed
  69.         try:
  70.             x = MyClass
  71.             y = object
  72.             self.assertIsInstance(type(x), y, "Not an Instance")
  73.         except AssertionError as e:
  74.             print(e)
  75.  
  76. if __name__ == '__main__':
  77.     # run unittest
  78.     unittest.main()

 

 

2.unittest test groups 

Calc.py

  1. class Calc(object):
  2.  
  3.     def add(self, *d):
  4.         #
  5.         result = 0
  6.         for i in d:
  7.             result += i
  8.         return result
  9.  
  10.     def mul(self, *d):
  11.         #
  12.         result = 1
  13.         for i in d:
  14.             result = result * i
  15.         return result
  16.  
  17.  
  18.     def sub(self, a, *d):
  19.     #
  20.         result = a
  21.         for i in d:
  22.             result = result - i
  23.         return result
  24.  
  25.  
  26.     def div(self, a, *d):
  27.     #
  28.         result = a
  29.         for i in d:
  30.             result = result / i
  31.         return result

 TestCalc.py

  1. import unittest
  2. import random
  3. from Calc import Calc
  4.  
  5. class TestCalcFunctions(unittest.TestCase):
  6.  
  7.     def setUp(self):
  8.         self.c = Calc()
  9.         print("setup completed!")
  10.  
  11.     def test_sum(self):
  12.         self.assertTrue(self.c.add(1, 2, 3, 4) == 10)
  13.  
  14.     def test_sub(self):
  15.     # fix a line of codes to test c.sub(self, a, *b) method
  16.         self.assertTrue(self.c.sub(4, 1, 2, 3) == -2)
  17.     def test_mul(self):
  18.     # fix a line of codes to test c.mul(self, *b) method
  19.         self.assertTrue(self.c.mul(3, 4) == 12)
  20.     def test_div(self):
  21.     # fix a line of codes to test c.div(self, a, *b) method
  22.         self.assertTrue(self.c.div(4, 2) == 2)
  23.     def tearDown(self):
  24.         print("test completed!")
  25.  
  26.     def tearDown(self):
  27.         print("tearDown completed")
  28.  
  29. if __name__ == '__main__':
  30.     unittest.main()

 

 unittest_suite.py

  1. import random
  2. import unittest
  3. from TestCalc import TestCalcFunctions
  4.  
  5.  
  6. class TestSequenceFunctions(unittest.TestCase):
  7.     def setUp(self):
  8.         self.seq = list(range(10))
  9.  
  10.     def tearDown(self):
  11.         pass
  12.  
  13.     def test_choice(self):
  14.         # chose an element from seq randomly
  15.         element = random.choice(self.seq)
  16.         # check element is truly in the sequence
  17.         self.assertTrue(element in self.seq)
  18.  
  19.     def test_sample(self):
  20.         # if codes raise exception
  21.         with self.assertRaises(ValueError):
  22.             random.sample(self.seq, 20)
  23.  
  24.         for element in random.sample(self.seq, 5):
  25.             self.assertTrue(element in self.seq)
  26.  
  27.  
  28. class TestDictValueFormatFunctions(unittest.TestCase):
  29.     def setUp(self):
  30.         self.seq = list(range(10))
  31.  
  32.     def tearDown(self):
  33.         pass
  34.  
  35.     def test_shuffle(self):
  36.         # shuffle sequence
  37.         random.shuffle(self.seq)
  38.         self.seq.sort()
  39.         self.assertEqual(self.seq, list(range(10)))
  40.         # check TypeError exception
  41.         self.assertRaises(TypeError, random.shuffle, (1, 2, 3))
  42.  
  43.  
  44. if __name__ == '__main__':
  45.     # get all test methods start with ‘test’ and return a suite
  46.     suite1 = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
  47.     # please fix another two suite, suite2 of TestCalcFunctions and suite3 of TestDictValueFormatFunctions
  48.     suite2 = unittest.TestLoader().loadTestsFromTestCase(TestCalcFunctions)
  49.     # put more test class into suite
  50.     suite3 = unittest.TestLoader().loadTestsFromTestCase(TestDictValueFormatFunctions)
  51.     # you can change suites’ order, like [suite1, suite2, suite3]
  52.     suite = unittest.TestSuite([suite2, suite1, suite3])
  53.     # set verbosity = 2 you could get more detailed information
  54.     unittest.TextTestRunner(verbosity=2).run(suite)

 

 3. unittest skip test

  1. # encoding=utf-8
  2.  
  3. import random, sys, unittest
  4.  
  5.  
  6. class TestSeqFunctions(unittest.TestCase):
  7.     a = 1
  8.  
  9.     def setUp(self):
  10.         self.seq = list(range(20))
  11.  
  12.     @unittest.skip("skipping")  # skip this method anyway
  13.     def test_shuffle(self):
  14.         random.shuffle(self.seq)
  15.         self.seq.sort()
  16.         self.assertEqual(self.seq, list(range(20)))
  17.         self.assertRaises(TypeError, random.shuffle, (1, 2, 3))
  18.  
  19.     # add a line of annotation that skip this method if a>5
  20.     @unittest.skipIf(a > 5,"skip this method if a>5")
  21.     def test_choice(self):
  22.         element = random.choice(self.seq)
  23.         self.assertTrue(element in self.seq)
  24.  
  25.     # add a line of annotation that skip if not in linux platform
  26.     @unittest.skipIf(sys.platform!='linux', "Require for linux")
  27.     def test_sample(self):
  28.         with self.assertRaises(ValueError):
  29.             random.sample(self.seq, 20)
  30.         for element in random.sample(self.seq, 5):
  31.             self.assertTrue(element in self.seq)
  32.  
  33.  
  34. if __name__ == "__main__":
  35.     # unittest.main()
  36.     suite = unittest.TestLoader().loadTestsFromTestCase(TestSeqFunctions)
  37.     suite = unittest.TestSuite(suite)
  38.     unittest.TextTestRunner(verbosity=2).run(suite)

 

 4. unittest Run test under numerical order or alpha order.

  1. # encoding=utf-8
  2.  
  3. import unittest
  4. from Calc import Calc
  5.  
  6. class MyTest(unittest.TestCase):
  7.     @classmethod
  8.     def setUpClass(self):
  9.         print("init Calc before unittest")
  10.         self.c = Calc()
  11.  
  12.         # P.S.: test case must starts with ‘test’
  13.     def test_a_add(self):
  14.         print("run add()")
  15.         self.assertEqual(self.c.add(1, 2, 12), 15, 'test add fail')
  16.  
  17.     def test_b_sub(self):
  18.         print("run sub()")
  19.         self.assertEqual(self.c.sub(2, 1, 3), -2, 'test sub fail')
  20.  
  21.     def test_c_mul(self):
  22.         print("run mul()")
  23.         self.assertEqual(self.c.mul(2, 3, 5), 30, 'test mul fail')
  24.  
  25.     def test_d_div(self):
  26.         print("run div()")
  27.         self.assertEqual(self.c.div(8, 2, 4), 1, 'test div fail')
  28.  
  29. if __name__ == '__main__':
  30.     unittest.main()

 

5. unittest Time Test 

  1. import sys
  2. import time
  3. import unittest
  4. import timeout_decorator
  5.  
  6. class timeoutTest(unittest.TestCase):
  7.     # set a time decorator(annotation) which will be triggered after 5s’ running
  8.     @timeout_decorator.timeout(5)
  9.     def testtimeout(self):
  10.         print("Start")
  11.         for i in range(1, 10):
  12.             time.sleep(1)
  13.             print("%d seconds have passed" % i)
  14.  
  15. if __name__ == "__main__":
  16.     unittest.main()

Conclusion

After this experiment, I had such a good use of understanding of some basic knowledge and applications of unit testing. Also had good chance to learn and review the usage of Python.

...全文
134 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

183

社区成员

发帖
与我相关
我的任务
社区描述
福州大学 梅努斯国际工程学院 软件工程 教学
软件工程 高校
社区管理员
  • 单步调试
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧