unittest 测试套 框架

本文介绍了使用Python编写的自动化测试脚本,包括测试框架的入口函数、BeautifulReport组件生成测试报告以及具体的测试用例,如Selenium驱动浏览器操作和断言功能。
摘要由CSDN通过智能技术生成

一.框架总览

报告预览图:

二、代码部分

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.skip(reason):强制skip。reason是跳转原因
  • @unittest.skipIf(condition, reason):condition为True的时候跳过
  • @unittest.skipUnless(condition, reason):condition为False的时候跳过
  • @unittest.expectedFailure:如果test fail,这个test不计数
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>测试页面</title>
  <style>
      body {
          font-family: Arial, sans-serif;
      }
  </style>
</head>
<body>
  <h1>测试页面——输入框</h1>
  <form>
      <label for="username">用户名:</label>
      <input type="text" id="username" name="username">
      <label for="password">密码:</label>
      <input type="password" id="password" name="password">
      <button id="submit" type="submit">提交</button>
  </form>
  
</body>
</html>

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值