EE308 Lab11

sunshine1211111 sunshine1211111 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

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.

...全文

相关推荐

EE-308 LAB4 final version.7z

这是本次实验的压缩包

晨起剪桃花

EE308-Lab11-Software Testing

unittest for Python

调参侠鱼尾

build-bluez-static.sh

# BlueZ 5.77 交叉编译脚本 (动态链接, 可在 OpenHarmony 上运行) # 工具链: gcc-arm-10.3-2021.07 (arm-none-linux-gnueabihf, glibc 2.33) # # 背景: OpenHarmony 使用 musl libc, 与 glibc 静态二进制不兼容 (NSS 问题) # 方案: 编译为动态链接, 打包 glibc 运行时 .so, 用自定义 ld-linux 启动

春风亭老朝1

国央企在开展内部创新项目时,如何有效进行技术成果的价值评估与资源筛选?.docx

科易网基于AI模型+行业大数据,在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,推出科技创新领域的AI+工具与数智化解决方案,推动科技创新与产业创新智能化发展。

BIZKEEN

电力系统风储联合一次调频仿真模型(Simulink仿真实现)

内容概要:本文详细介绍了一个基于Simulink平台构建的电力系统风储联合一次调频仿真模型,重点实现了风电与储能系统协同参与电网频率调节的动态过程。模型核心技术涵盖三电平ANPC并网逆变器控制、DPWMA调制策略、正负序分离与电网前馈控制,并引入虚拟同步发电机(VSG)控制以增强系统惯性与频率响应能力。仿真体系包含双闭环控制、中点电位平衡、虚拟阻抗、统一有源阻尼、孤岛检测等功能模块,支持在不平衡电网、弱电网等复杂工况下进行系统稳定性分析与先进控制策略验证,特别适用于低惯量电力系统中的一次调频研究与SCI级论文成果复现。; 适合人群:面向具备电力电子、电力系统自动化或新能源并网控制背景的研究生、科研人员及工程技术开发者;要求熟悉Matlab/Simulink仿真环境,有逆变器控制或微网运行研究经验者更佳。; 使用场景及目标:①开展风电与储能协同参与电网一次调频的控制策略研究;②验证VSG、下垂控制、模型预测控制(MPC)等先进控制方法在构网型逆变器中的动态性能;③复现高水平期刊论文中的电磁暂态仿真案例,支撑科研立项、论文撰写与工程项目开发。; 阅读建议:建议结合提供的参考文献与配套仿真资料,逐步搭建并调试各功能模块,重点关注控制环路设计、参数整定与阻抗建模方法,通过对比不同扰动工况下的仿真结果,深入理解系统动态响应特性与稳定性机理。

欢多天天写代码
评论
收藏 已收藏
分享
返回顶部