EE308 LAB11
|
The Link Your Class | |
|
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 |
19104154_831901227 |
1. unittest assert test
import unittest,random
# Test Class
class MyClass(object):
@classmethod
def sum(self, a, b):
return a + b
@classmethod
def div(self, a, b):
return a / b
@classmethod
def return_None(self):
return None
# Unit Test Class
class MyTest(unittest.TestCase):
# assertEqual()
def test_assertEqual(self):
# test if a+b equals sum or not
try:
a, b = 1, 2
sum = 3
self.assertEqual(a + b, sum, 'assert failed!,%s + %s != %s' %(a, b, sum))
except AssertionError as e:
print (e)
# assertNotEqual()
def test_assertNotEqual(self):
# fix missing three lines of codes below ‘try’, test if b-a equals res or not
try:
a, b = 5,2
res = 1
self.assertNotEqual(a - b, res, 'assert failed!,%s - %s != %s' % (a, b, res))
except AssertionError as e:
print (e)
# assertTrue()
def test_assertTrue(self):
try:
self.assertTrue(1 == 1, "False expression")
except AssertionError as e:
print (e)
# assertFalse()
def test_assertFalse(self):
# fix missing codes below ‘try’, only a line of codes needed
try:
self.assertFalse(1 == 2, "True expression")
except AssertionError as e:
print (e)
# assertIs()
def test_assertIs(self):
# test a and b are totally same
try:
a = 12
b = a
self.assertIs(a, b, "%s and %s are not same" %(a, b))
except AssertionError as e:
print (e)
# assertIsInstance()
def test_assertIsInstance(self):
# fix missing codes below ‘y=object’ to test type(x) != y, only a line of codes needed
try:
x = MyClass
y = object
self.assertIsInstance(x, y, "%s and %s are not the same type" % (x, y))
except AssertionError as e:
print (e)
if __name__ == '__main__':
# run unittest
unittest.main()

2. unittest test groups
Calc.py
class Calc(object):
def add(self, *d):
#
result = 0
for i in d:
result += i
return result
def mul(self, *d):
#
result =1
for i in d:
result = result*i
return result
def sub(self,a, *d):
#
result =a
for i in d:
result = result-i
return result
def div(self, a, *d):
#
result =a
for i in d:
result = result/i
return result
TestCalc.py
import unittest
import random
from Calc import Calc
class TestCalcFunctions(unittest.TestCase):
def setUp(self):
self.c=Calc()
print ("setup completed!")
def test_sum(self):
self.assertTrue(self.c.add(1,2,3,4)==10)
def test_sub(self):
# fix a line of codes to test c.sub(self, a, *b) method
self.assertTrue(self.c.sub(1, 2, 3, 4) == -8)
def test_mul(self):
# fix a line of codes to test c.mul(self, *b) method
self.assertTrue(self.c.mul(1, 2, 3, 4) == 24)
def test_div(self):
# fix a line of codes to test c.div(self, a, *b) method
self.assertTrue(self.c.div(24, 2, 3, 4) == 1)
def tearDown(self):
print ("test completed!")
def tearDown(self):
print ("tearDown completed")
if __name__ == '__main__':
unittest.main()

unittest_suite.py
import random
import unittest
from TestCalc import TestCalcFunctions
class TestSequenceFunctions(unittest.TestCase):
def setUp(self):
self.seq = list(range(10))
def tearDown(self):
pass
def test_choice(self):
# chose an element from seq randomly
element = random.choice(self.seq)
# check element is truly in the sequence
self.assertTrue(element in self.seq)
def test_sample(self):
# if codes raise exception
with self.assertRaises(ValueError):
random.sample(self.seq, 20)
for element in random.sample(self.seq, 5):
self.assertTrue(element in self.seq)
class TestDictValueFormatFunctions(unittest.TestCase):
def setUp(self):
self.seq = list(range(10))
def tearDown(self):
pass
def test_shuffle(self):
# shuffle sequence
random.shuffle(self.seq)
self.seq.sort()
self.assertEqual(self.seq, list(range(10)))
# check TypeError exception
self.assertRaises(TypeError, random.shuffle, (1, 2, 3))
if __name__ == '__main__':
# get all test methods start with ‘test’ and return a suite
suite1 = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
# please fix another two suite, suite2 of TestCalcFunctions and suite3 of TestDictValueFormatFunctions
suite2 = unittest.TestLoader().loadTestsFromTestCase(TestCalcFunctions)
suite3 = unittest.TestLoader().loadTestsFromTestCase(TestDictValueFormatFunctions)
# put more test class into suite
# you can change suites’ order, like [suite1, suite2, suite3]
suite = unittest.TestSuite([suite2, suite1,suite3])
# set verbosity = 2 you could get more detailed information
unittest.TextTestRunner(verbosity = 2).run(suite)

3. unittest skip test
#encoding=utf-8
import random,sys,unittest
class TestSeqFunctions(unittest.TestCase):
a = 1
def setUp(self):
self.seq = list(range(20))
@unittest.skip("skipping") # skip this method anyway
def test_shuffle(self):
random.shuffle(self.seq)
self.seq.sort()
self.assertEqual(self.seq,list(range(20)))
self.assertRaises(TypeError,random.shuffle,(1,2,3))
# add a line of annotation thatskip this method if a>5
@unittest.skipIf(a>5, "skipping if a>5")
def test_choice(self):
element = random.choice(self.seq)
self.assertTrue(element in self.seq)
# add a line of annotation that skip if not in linux platform
@unittest.skipIf(sys.platform != "linux*", "skipping if not in linux platform")
def test_sample(self):
with self.assertRaises(ValueError):
random.sample(self.seq, 20)
for element in random.sample(self.seq, 5):
self.assertTrue(element in self.seq)
if __name__=="__main__":
# unittest.main()
suite = unittest.TestLoader().loadTestsFromTestCase(TestSeqFunctions)
suite = unittest.TestSuite(suite)
unittest.TextTestRunner(verbosity = 2).run(suite)

4.unittest Run test under numerical order or alpha order.
#encoding=utf-8
import unittest
from Calc import Calc
class MyTest(unittest.TestCase):
@classmethod
def setUpClass(self):
print ("init Calc before unittest")
self.c = Calc()
def test_1_add(self):
print ("run add()")
self.assertEqual(self.c.add(1, 2, 12), 15, 'test add fail')
def test_2_sub(self):
print ("run sub()")
self.assertEqual(self.c.sub(2, 1, 3), -2, 'test sub fail')
def test_3_mul(self):
print ("run mul()")
self.assertEqual(Calc.mul(2, 3, 5), 30, 'test mul fail')
def test_4_div(self):
print ("run div()")
self.assertEqual(Calc.div(8, 2, 4), 1, 'test div fail')
if __name__ == '__main__':
unittest.main()

5. unittest Time Test
import time
import timeout_decorator
class timeoutTest(unittest.TestCase):
# set a time decorator(annotation) which will be triggered after 5s’ running
@timeout_decorator.timeout(5)
def testtimeout(self):
print "Start"
for i in range(1,10):
time.sleep(1)
print "%d seconds have passed" % i
if __name__ == "__main__":
unittest.main()
相关推荐
EE-308 LAB4 final version.7z
【标签】"EE-308 LAB4" 是一个明确的分类标签,其中"EE-308"可能代表一门具体的大学课程,如“电子系统”或者“数字信号处理”。
EE308-Lab11-Software Testing
unittest for Python
弱电网下光伏并网逆变器序阻抗建模仿真复现扫频法【序阻抗判断稳定性】
内容概要:本文聚焦于弱电网条件下光伏并网逆变器的序阻抗建模与稳定性分析,系统阐述了基于扫频法的阻抗特性仿真与复现方法。通过构建正负序阻抗模型,深入剖析逆变器在弱电网环境中的动态响应特性,结合锁相环、电流环等关键控制环节对系统阻抗的影响机制,采用序阻抗稳定性判据评估多机并网或复杂电网条件下的系统稳定裕度。研究在Simulink平台中实现激励信号注入、频率响应采集与阻抗曲线绘制,为高比例新能源接入背景下并网系统的稳定性分析提供了可靠的仿真手段与理论支撑,尤其适用于SCI级别论文成果的复现与拓展验证。; 适合人群:电力电子、新能源并网技术及相关领域的硕士/博士研究生、科研人员,以及从事光伏逆变器、微电网稳定控制开发的工程技术人员。; 使用场景及目标:①掌握扫频法在电力电子装置阻抗建模中的具体实施流程;②理解并应用基于序阻抗的稳定性判据进行系统稳定边界分析;③用于高水平学术论文(如SCI期刊)中阻抗建模与稳定性分析部分的仿真复现与方法验证; 阅读建议:建议读者结合文中所述方法与提供的参考文献,自主搭建Simulink仿真模型,逐步完成扰动信号设计、数据采集、傅里叶分析及阻抗拟合全过程,深入探究控制参数变化对系统阻抗特性和稳定性的具体影响,强化理论与实践的融合能力。
三相并网逆变器虚拟阻抗+统一有源阻尼策略SVPWM+SPWM调制仿真
内容概要:本文档系统性地介绍了三相并网逆变器的核心控制技术及其在复杂电网环境下的仿真应用,重点涵盖虚拟阻抗与统一有源阻尼策略在SVPWM和SPWM调制方式下的实现方法,旨在提升逆变器的稳定性与电能质量。内容包括无功功率扰动孤岛检测、中点电位平衡控制、低电压穿越(LVRT)、模型预测控制(MPC)、正负序分离、电网前馈控制等关键技术,并扩展至风储联合调频、微电网下垂控制、虚拟同步发电机(VSG)、构网型逆变器等多种典型电力系统应用场景。所有技术均配有基于Simulink/Matlab平台的完整仿真模型、参考文献及说明文档,强调理论分析与仿真实践的深度融合,突出科研中“借力”与创新思维的重要性。; 适合人群:电气工程、自动化、新能源等相关专业的研究生、科研人员及从事电力电子与可再生能源系统开发的工程师;具备MATLAB/Simulink仿真基础和电力系统专业知识的技术人员。; 使用场景及目标:①开展三相并网逆变器控制策略的研究与优化设计;②复现高水平学术论文中的控制算法;③完成毕业课题、科研项目或工程仿真验证;④掌握虚拟阻抗、有源阻尼、SVPWM/SPWM调制、MPC、VSG等核心技术的建模与实现方法;⑤支撑微电网、构网型变流器等前沿方向的科研攻关。; 阅读建议:建议按照资源目录结构化学习,优先选择与自身研究方向契合的内容模块,结合提供的仿真模型、参考文献和说明材料进行实操演练,注重从理论推导到仿真实现的全过程理解,充分利用共享网盘资源深化技术掌握,提升科研效率与创新能力。
chromedriver-win32-159.0.8133.0(Canary).zip
chromedriver-win32-159.0.8133.0(Canary).zip


