欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

unittest框架初识

程序员文章站 2024-03-24 10:29:28
...

新建3个文件夹,分别是case、function、test
function文件夹中保存待测试的类

from selenium import webdriver
import os
import re

class getsong:
    def getsongmessage(url):
        # content = re.compile('qq.com\/ (.*)\.m4a\?').findall(url)[0]
        content = re.compile('C400(.*)\.m4a').findall(url)[0]
        print(content)
        geturl = r'https://y.qq.com/n/yqq/song/' + content + r'.html'

        print(geturl)
        driver = webdriver.Chrome()
        driver.get(geturl)

        songname = driver.find_element_by_class_name("data__name_txt").text
        singername = driver.find_element_by_class_name("data__singer").text
        albumname = driver.find_element_by_class_name("js_album").text
        driver.quit()
        msg = {
            'songname': songname,
            'singername': singername,
            'albumname': albumname
        }
        print("歌曲名称为:", songname)
        print("歌手名称为:", singername)
        print("专辑名称为:", albumname)
        return msg



# if __name__ == '__main__':
#     url =r'http://111.202.98.148/amobile.music.tc.qq.com/C400003OUlho2HcRHC.m4a?guid=1907614314&vkey=AAA53A6F530A4EACF02F4FAEEA284959F3947FBBE7178875A2916DA14AA315E295090FA10DAA6EB728CE4751AF3AA188170F1F3EAD5BF281&uin=1520&fromtag=66'
#     print(type(url))
#     getsongmessage(url)

case中保存对应的单元测试case
用例一:

import unittest
from function import getMsg

class testgetMsg(unittest.TestCase):

    def setUp(self):
        print("test start")

    def test_getMsg(self):
        b = {
            'songname': "告白气球",
            'singername': "周杰伦",
            'albumname': "周杰伦的床边故事"
        }
        a = getMsg.getsong.getsongmessage(r'http://111.202.98.148/amobile.music.tc.qq.com/C400003OUlho2HcRHC.m4a?guid=1907614314&vkey=AAA53A6F530A4EACF02F4FAEEA284959F3947FBBE7178875A2916DA14AA315E295090FA10DAA6EB728CE4751AF3AA188170F1F3EAD5BF281&uin=1520&fromtag=66')
        self.assertEqual(a,b)


    def tearDown(self):
        print("test end")


用例二:

import unittest
from function import getMsg


class testgetMsg2(unittest.TestCase):

    def setUp(self):
        print("test start")

    def test_getMsg2(self):
        b = {
            'songname': "卡路里 (Live)",
            'singername': "腾格尔",
            'albumname': "2019湖南卫视元宵喜乐会"
        }
        a =  getMsg.getsong.getsongmessage(
            r'http://111.202.98.148/amobile.music.tc.qq.com/C400002jG4Rv2DeC3O.m4a?guid=1907614314&vkey=AAA53A6F530A4EACF02F4FAEEA284959F3947FBBE7178875A2916DA14AA315E295090FA10DAA6EB728CE4751AF3AA188170F1F3EAD5BF281&uin=1520&fromtag=66')
        self.assertEqual(a, b)

    def tearDown(self):
        print("test end")

test则用于启动测试

import unittest
from  case import testgetMsg
from  case import testgetMsg2


#构造测试集
gogogo = unittest.TestSuite()

gogogo.addTest(testgetMsg.testgetMsg("test_getMsg"))
gogogo.addTest(testgetMsg2.testgetMsg2("test_getMsg2"))



if __name__ == '__main__':
    runner = unittest.TextTestRunner()
    runner.run(gogogo)