EE308 LAB11

two 吐 兔 two 吐 兔 墨衍会员 · 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/603737050

The Aim of This Assignment

Software Testing

MU STU ID and FZU STU ID

19104529_831901217

1. Aim

 This experiment is used to record the test process, summarize the test conditions of each round, and analyze the test data. Here, I use Python for convenience, so unittest task is my choice.

2.Task flow

  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 retrun_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 = 1, 2
            res = 1
            # Using assertNotEqual() to prove 2-1=1
            self.assertNotEqual(b-a,res,'assert true!,%s - %s == %s' %(b, a, 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:
            # Using assertFalse() to prove 1==1 reversely
            self.assertFalse(1 != 1, "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
            # Using assertIs() to prove obj is not same
            self.assertIs(x, y, "type(%s) and %s are not same" % (x, y))
        except AssertionError as e:
            print(e)

    if __name__ == '__main__':
        # run unittest
        unittest.main()

Thus the result is:

 2.unittest test groups

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



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
        # Using assertFalse() to prove the -8 !=10
        self.assertFalse(self.c.sub(1, 2, 3, 4) == 10)

    def test_mul(self):
        # fix a line of codes to test c.mul(self, *b) method
        # Using assertTrue() to prove the 24 ==24
        self.assertTrue(self.c.mul(2, 3, 4) == 24)

    def test_div(self):
        # fix a line of codes to test c.div(self, a, *b) method
        # Using assertFalse() to prove the 1/24 !=10
        self.assertFalse(self.c.div(1, 2, 3, 4) == 10)

    def test(self):
        print("test completed!")

    def tearDown(self):
        print("tearDown completed")


if __name__ == '__main__':
    unittest.main()




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)
    suite2 = unittest.TestLoader().loadTestsFromTestCase(TestCalcFunctions)
    suite3 = unittest.TestLoader().loadTestsFromTestCase(TestDictValueFormatFunctions)
    # please fix another two suite, suite2 of TestCalcFunctions and suite3 of 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)

 Thus, the result is:

 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
    # Using skipif() If the condition is true, continue to execute, otherwise skip the decorated test case;
    @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
    # Using skipif() If the condition is true, continue to execute, otherwise skip the decorated test case;
    # Using sys.platform to generate the platform information
    @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)

Thus, the result is:

 

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()
    # Using the test_(number) to sort program from large to small
    def test_1add(self):
        print("run add()")
        self.assertEqual(self.c.add(1, 2, 12), 15, 'test add fail')

    def test_2sub(self):
        print("run sub()")
        self.assertEqual(self.c.sub(2, 1, 3), -2, 'test sub fail')

    def test_3mul(self):
        print("run mul()")
        self.assertEqual(Calc.mul(1,2, 3, 5), 30, 'test mul fail')

    def test_4div(self):
        print("run div()")
        self.assertEqual(Calc.div(1,8, 2, 4), 1, 'test div fail')

if __name__ == '__main__':
    unittest.main()

Thus, the result is:

5.unittest Time Test

import time
import unittest
import timeout_decorator


class timeoutTest(unittest.TestCase):

    # set a time decorator(annotation) which will be triggered after 5s’ running
    # Using the decorator library to stop 5 seconds
    @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()

 Thus, the result is:

For the error that occurs, is because the  Limitations of the code library as well as the limitions of the Time overflow

Conclusion

 After several rounds of testing in Python, we have been basically familiar with the basic functions in unittest library and other related libraries, applied them systematically, preliminarily completed the experimental requirements, and left some remaining problems to be further studied

...全文

相关推荐

EE-308 LAB4 final version.7z

这是本次实验的压缩包

晨起剪桃花

EE308-Lab11-Software Testing

unittest for Python

调参侠鱼尾

【自然语言处理】基于CRISPE与BROKE框架的提示词工程应用:大语言模型内容生成中的写作方法优化研究

内容概要:本文探讨提示词工程在人工智能内容生成中的关键作用,以CRISPE与BROKE两个提示词框架为例,分析其在角色设定、背景约束、验收标准和迭代机制上的异同,并通过撰写“AI与就业”科普短文的实际案例,展示提示词从模糊任务到可执行指令的构建过程。研究指出,提示词工程的核心不在于模板记忆,而在于提升意图的清晰度、约束的精确性、输出的可评测性以及对模型能力边界的认知。文章强调,有效的提示需明确角色、背景、任务、风格与验收标准,并通过多轮实验与迭代优化结果,避免生成内容陷入形式化与模板化。; 适合人群:具备一定AI基础知识、正在学习或使用大语言模型进行内容创作的学生、研究人员及内容创作者,尤其是关注写作质量与个性化表达的用户。; 使用场景及目标:①掌握如何将模糊写作意图转化为结构化提示词;②提升AI生成内容的信息密度、可读性与实用性;③在科研写作、科普传播等场景中保留个人思考痕迹,避免机械化表达;④学会通过可检查的验收标准持续优化输出。; 阅读建议:此资源不仅介绍提示词框架的操作方法,更强调思维训练。建议读者结合自身写作任务,动手实践CRISPE与BROKE的组合应用,重点关注约束设计与结果评测环节,并反思“为何提示无效”,从而真正实现从“会问”到“会写”的跃迁。

beiningya

技术转移机构自建平台与使用第三方SaaS系统相比,哪种投入产出比更好?.docx

科易网(厦门科易网科技有限公司)创立于2007年,是技术转移与科技创新数智化服务平台,拥有国家科技成果转化服务示范基地、国家技术转移示范机构等资质。平台以“AI+科技成果转化”为核心,整合成果、专利、专家、政策等多维数据,构建知识图谱与科创智能体,面向企业、政府、高校院所提供成果转化、技术需求对接、产业服务等数智化解决方案。截至目前,已服务全国企业超43万家,推动落地项目11800余项,总金额逾80亿元。

BIZKEEN

2026年中国重点行业企业研发投入强度与创新产出比的数据分布现状是怎样的?.docx

2026年中国重点行业企业研发投入强度与创新产出比的数据分布现状是怎样的?

BIZKEEN
评论
收藏 已收藏
分享
返回顶部