EE308_lab11

Huang_Junqi 2021-12-17 15:38:25
The Link Your Classhttps://bbs.csdn.net/forums/MUEE308FZ
The Link of Requirement of This AssignmentLAB 11 Software Testing-CSDN社区
The Aim of This Assignmentunitttest
MU STU ID and FZU STU ID19105169_831901316

Unittest

part 1

In the first part, we learned how to use assert

In assertion testing, only failed assertion tests are recorded.Attach the completed code.

import unittest, random


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 = 2, 2
            sum = 3
            self.assertNotEqual(a + b, sum, 'assert failed!,%s + %s = %s' % (a, b, sum))
        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, "False 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(type(x), y, "given object is not a instance of class")

        except AssertionError as e:
            print(e)


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

part 2

 unittest test groups.

Use assertTure to unit test code from other Python files。

# 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):
        self.assertTrue(self.c.sub(10, 2) == 8)

    # fix a line of codes to test c.sub(self, a, *b) method

    def test_mul(self):
        self.assertTrue(self.c.mul(11, 2) == 22)

    # fix a line of codes to test c.mul(self, *b) method

    def test_div(self):
        self.assertTrue(self.c.div(10,2) == 5)

    # fix a line of codes to test c.div(self, a, *b) method

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

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

part 3

unittest_suite

Use the test suite to drive a test set.The completed code is attached here


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)

part 4

In some cases, skip the 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))

    @unittest.skipIf(a > 5, "skip if a>5")
    def test_choice(self):
        element = random.choice(self.seq)
        self.assertTrue(element in self.seq)

    @unittest.skipIf(sys.platform != "linux", "skipping if system is linux")
    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)

part 5

In order for the test program to proceed in a certain order, we must rename the names of the test code segments in the order of ASCII.Here I use 1 2 3 4 as the order


import unittest
from Calc import Calc


class MyTest(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        print("init Calc before unittest")
        self.c = Calc()

    # rename the four methods bellow, make sure print queue will be :

    # P.S.: test case must starts with ‘test’
    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(self.c.mul(2, 3, 5), 30, 'test mul fail')

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


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

part 6

unittest Time Test

import time
import unittest
import timeout_decorator


class timeoutTest(unittest.TestCase):

    @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()

 

...全文
154 回复 打赏 收藏 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

183

社区成员

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

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