EE308_LAB11
| 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 Test -Unittest |
| MUID&FZUID | 19105789_831901108 |
Experiment Requirements
- You should be familiar with Junit Test or Unittest and the usage of these tools.Such as ‘assert, suite, timeout’, check problems for detail.
- Fix the missing codes according to requirement for every problem.
Aim
Learn related knowledge about Junit Test for Java and Unittest for python to do the Softeware Testing.
目录
4. Unittest Run Test Under Numerical order or Alpha order
1.Unittest Assert Test
# Test Class
class Class(object):
@classmethod
def sum(p, a, b):
return a + b
@classmethod
def div(p, a, b):
return a / b
@classmethod
def retrun_None(p):
return None
# Unit Test Class
class Test (unittest.TestCase):
def test_ifEqual(p):
# test if a+b equals sum or not
a, b = 1, 2
sum = 3
p.ifEqual(a + b, sum, 'failed,%s + %s != %s' %(a, b, sum))
#except AssertError as e:
print (e)
# assertNotEqual()
def test_NotEqual(p):
# test if b-a equals res or not
a, b = 2, 1
res = 1
p.ifEqual(b - a, res, 'failed,%s - %s != %s' %(a, b, res))
#except AssertError as e:
print (e)
# ifTrue()
def test_ifTrue(p):
p.ifTrue(1 == 1, "False")
# except AssertError as e:
print (e)
# ifFalse()
def test_ifFalse(p):
# fix missing codes below ‘try’, only a line of codes needed
p.ifFalse(1 == 2, "False")
# except AssertError as e:
print (e)
# assertIs()
def test_assertIs(p):
# test a and b are totally same
a = 12
b = a
p.assertIs(a, b, "%s and %s are not same" %(a, b))
# except AssertionError as e:
print (e)
# assertIsInstance()
def test_assertIsInstance(p):
# fix missing codes below ‘y=object’ to test type(x) != y, only a line of codes needed
x = Class
y = object
p.assertIsInstance(y, x, "False")
# except AssertionError as e:
print (e)
if __name__ == '__main__':
# run unittest
unittest.main()
<question 2>
文件1
#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
2. Unittest Test Groups
class Calc(object):
def add(p, *d):
#
result = 0
for i in d:
result += i
return result
def mul(p, *d):
#
result =1
for i in d:
result = result*i
return result
def sub(p,a, *d):
#
result =a
for i in d:
result = result-i
return result
def div(p, 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(p):
p.c=Calc()
print ("Setup complete!")
def test_sum(p):
p.ifTrue(p.c.add(1,2,3,4)==10)
def test_sub(p):
# fix a line of codes to test c.sub(p, a, *b) method
p.ifTrue(self.c.sub(4,3)==1)
def test_mul(p):
# fix a line of codes to test c.mul(p, *b) method
p.ifTrue(p.c.mul(4,3,2)==24)
def test_div(p):
# fix a line of codes to test c.div(p, a, *b) method
p.ifTrue(p.c.div(12,3,2)==2)
def tearDown(p):
print ("Test complete!")
def TearDown(p):
print ("TearDown complete!")
if __name__ == '__main__':
unittest.main()
# unittest_suite.py
import random
import unittest
from TestCalc import TestCalcFunctions
class TestSequenceFunctions(unittest.TestCase):
def setUp(p):
p.seq = list(range(10))
def TearDown(p):
pass
def test_choice(p):
# chose an element from seq randomly
el = random.choice(p.seq)
# check element is truly in the sequence
p.ifTrue(el in self.seq)
def test_sample(p):
# if codes raise exception
with self.assertRaises(ValueError):
random.sample(p.seq, 20)
for element in random.sample(p.seq, 5):
p.assertTrue(el in p.seq)
class TestDictValueFormatFunctions(unittest.TestCase):
def setUp(p):
p.seq = list(range(10))
def tearDown(p):
pass
def test_shuffle(p):
# shuffle sequence
random.shuffle(p.seq)
p.seq.sort()
p.assertEqual(p.seq, list(range(10)))
# check TypeError exception
p.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
import random,sys,unittest
class TestSeqFunctions(unittest.TestCase):
a = 1
def setUp(p):
p.seq = list(range(20))
@unittest.skip("skipping")
def test_shuffle(p):
random.shuffle(p.seq)
p.seq.sort()
p.assertEqual(p.seq,list(range(20)))
p.assertRaises(TypeError,random.shuffle,(1,2,3))
@unittest.skipIf(a > 5, "skipping")
def test_choice(p):
element = random.choice(p.seq)
p.assertTrue(el in p.seq)
@unittest.skipUnless(sys.platform.startswith("linux"))
def test_sample(p):
with self.assertRaises(ValueError):
random.sample(p.seq, 20)
for element in random.sample(p.seq, 5):
self.assertTrue(el in p.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
import unittest
from Calc import Calc
class MyTest(unittest.TestCase):
@classmethod
def setUpClass(p):
print ("init Calc before unittest")
p.c = Calc()
def test_toAdd(p):
print ("run toAdd()")
p.ifEqual(p.c.add(1, 2, 12), 15, 'Add failed')
def test_toSub(p):
print ("run toSub()")
p.ifEqual(p.c.sub(2, 1, 3), -2, 'Subtract failed')
def test_toMul(p):
print ("run toMul()")
p.ifEqual(Calc.mul(2, 3, 5), 30, 'Multiply fail')
def test_3div(p):
print ("run toDiv()")
p.ifEqual(Calc.div(8, 2, 4), 1, 'Division 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(P):
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
这是本次实验的压缩包
EE308-Lab11-Software Testing
unittest for Python
EE308-Lab8-AlphaSprint Collection
EE308 Lab8 Alpha Sprint Collection1. Format Description2. 宁可累死自己,也要卷死同学 -Sprint Collection 1. Format Description The Link Your Class https://bbs.csdn.net/forums/MUEE308FZ The Link of Requirement of This Assignment https://bbs.csdn.net/topics/603251
【信息科学与工程学】【物理/化学科学和工程技术】知识体系073——电学基础04
。
【多无人机协同目标运输任务】多无人机协同目标运输任务中的路径规划与动态控制研究(Matlab代码实现)
内容概要:本文聚焦于多无人机协同执行目标运输任务中的关键问题,系统研究了复杂环境下多无人机系统的路径规划与动态控制策略,并提供了基于Matlab的完整仿真代码实现。研究内容包括多智能体协同机制设计、高效路径规划算法(如改进RRT*、A*等)的应用、动态响应控制策略(如反步控制、滑模控制等)的集成,以及任务过程中无人机间的协调控制逻辑。通过高保真度仿真验证了所提方法在不同场景下的有效性、鲁棒性与实用性,代码配备详尽注释,便于理解与二次开发。; 适合人群:具备一定控制理论基础和Matlab编程能力,从事无人机控制、多智能体系统、智能交通或机器人协同等方向的科研人员及研究生(工作1-3年或研一以上水平);; 使用场景及目标:①用于多无人机协同运输任务的算法设计与仿真验证;②支撑科研论文复现、课题开发及工程原型构建;③深入理解路径规划与动态控制在多机协同中的耦合关系与实现方式;; 阅读建议:建议结合Matlab代码逐模块调试运行,配合控制理论教材深化对控制律设计的理解;建议先掌握单无人机基本控制原理,再逐步过渡到多机协同逻辑的学习,以提升整体系统认知与实践效率。


