unittest 测试套 框架

一.框架总览

unittest 测试套 框架_第1张图片

报告预览图:

unittest 测试套 框架_第2张图片

二、代码部分

1.程序入口

import time
from colorama import Fore, Back, Style
from util.reportOut import report_out
import os


def run_case():
    print(Fore.BLUE + "[ATS][Start Now]" + Style.RESET_ALL)
    ospath = os.path.dirname(os.path.realpath(__file__))
    report_dir = os.path.join(ospath, "report")  # 测试报告存放目录
    test_dir = os.path.join(ospath, "testcases")  # 测试用例读取目录
    name_project = "Baidu"
    report_out(test_dir, report_dir, name_project)
    time.sleep(1.2)
    print(Fore.BLUE + "[ATS][End Now]" + Style.RESET_ALL)


if __name__ == '__main__':
    run_case()

2.组件部分

import time
import unittest
from BeautifulReport import BeautifulReport as bf


def report_out(test_dir, report_dir, name_project):
    """
    :test_dir: 用例dir
    :report_dir : 报告dir
    :name_project : 项目名称
    :return: 无
    """

    now = time.strftime("%Y_%m_%d#%H_%M_%S")
    runCover = unittest.defaultTestLoader.discover(test_dir, pattern='test*.py')  # 加载测试类
    report_name = name_project + '_' + now + '_TestReport.html'  # 测试报告
    run = bf(runCover)
    run.report(filename=report_name, report_dir=report_dir, description=U"BlueArmy测试")

3.测试用例

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import unittest
from _pytest.python_api import approx


class TestClass(unittest.TestCase):

    # driver = webdriver.Chrome()
    #
    # driver = None
    #
    # @classmethod
    # def setUpClass(cls) -> None:
    #
    #
    # @classmethod
    # def tearDownClass(cls) -> None:
    #     cls.driver.quit()

    def test_01_search(self):
        """判断"""
        assert 1 == 1

    def test_02_windows_size(self):
        """浏览器访问百度"""
        driver = webdriver.Firefox()
        url = r"D://Test\index1.html"
        driver.get(url)

        # 获取输入框元素
        username_input = driver.find_element(By.ID, "username")
        password_input = driver.find_element(By.ID, "password")

        # 输入用户名和密码
        username_input.send_keys("username")
        time.sleep(1.2)
        password_input.send_keys("password")
        time.sleep(1.2)
        # 提交表单
        driver.find_element(By.ID, "submit").click()

        time.sleep(1.2)
        # 等待页面加载完成
        driver.implicitly_wait(10)
        t = driver.title
        print(t)
        # 断言网页上的文本是否为预期值
        assert driver.title == "测试页面"

        # 关闭浏览器
        driver.quit()

    def test_03_assert(self):
        """判断浮点数"""
        assert 1.9 - 0.1 == approx(1.8)

    def test_04_assert(self):
        """判断浮点数失败"""
        assert 1.9 - 0.5 == approx(1.8)

    @unittest.skip(reason="接口未完善")
    def test_05_assert(self):
        """接口未完善失败skip"""
        assert 1.9 - 0.5 == approx(1.8)


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

附录:被测网页

unittest 测试套 框架_第3张图片

  • @unittest.skip(reason):强制skip。reason是跳转原因
  • @unittest.skipIf(condition, reason):condition为True的时候跳过
  • @unittest.skipUnless(condition, reason):condition为False的时候跳过
  • @unittest.expectedFailure:如果test fail,这个test不计数



  
  测试页面
  


  

测试页面——输入框

你可能感兴趣的:(python程序,unittest,python,集成测试)