ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

执行多线程unittest和pytest

2021-12-10 01:34:31  阅读:177  来源: 互联网

标签:__ os unittest pytest path 多线程 dr self DIR


#codin=utf-8

from selenium import webdriver
from selenium.webdriver.common.by import By
import time, unittest

class TEST1(unittest.TestCase):

    # 类方法(不需要实例化类就可以被类本身调用)
    @classmethod
    def setUpClass(cls): # cls : 表示自身类的 cls 参数,可以来调用类的属性,类的方法,实例化对象等。
        cls.dr = webdriver.Chrome()

    # 实例化方法(必须实例化类之后才能被调用)
    def test_01(self):  # self : 表示实例化类后的地址id
        self.dr.get("http://www.baidu.com")
        self.dr.find_element(By.ID, "kw").send_keys("线程一")
        self.dr.find_element(By.ID, "su").click()
        time.sleep(3)
        assert self.dr.title == "线程一_百度搜索"

    def test_02(self):
        self.dr.get("http://www.baidu.com")
        self.dr.find_element(By.ID, "kw").send_keys("第二个线程")
        self.dr.find_element(By.ID, "su").click()
        time.sleep(3)
        assert self.dr.title == "第二个线程_百度搜索"

    def test_03(self):
        self.dr.get("http://www.baidu.com")
        self.dr.find_element(By.ID, "kw").send_keys("第3个线程")
        self.dr.find_element(By.ID, "su").click()
        time.sleep(3)
        assert self.dr.title == "第二个线程_百度搜索"

    @classmethod
    def tearDownClass(cls):
        cls().dr.quit()

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

unittest执行

#coding=utf-8

import unittest, os, HTMLTestRunner
from tomorrow3 import threads

BASE_DIR = os.path.dirname(os.path.realpath(__file__))
# print(BASE_DIR)
TEST_DIR = os.path.join(BASE_DIR, "test_dir")
REPORT_DIR = os.path.join(BASE_DIR, "test_report")

def test_suits():
    """
    加载所有测试用例
    """
    discover = unittest.defaultTestLoader.discover(
        TEST_DIR,
        pattern="test_*.py"
    )
    return discover

@threads(3) #设置线程数
def run_case(all_case, nth=0):
    report_path = os.path.join(REPORT_DIR, "result{0}.html".format(nth))
    with open(report_path, "wb") as file:
        runner = HTMLTestRunner.HTMLTestRunner(stream=file, title="多线程报告")
        runner.run(all_case)

if __name__ == "__main__":
    cases = test_suits()
    for i, j in zip(cases, range(len(list(cases)))):
        run_case(i, nth=j)

pytest执行

#coding=utf-8

import os, pytest,time

#当前文件所在目录
BASE_DIR = os.path.dirname(os.path.realpath(__file__))
# print(BASE_DIR)
#测试文件目录
TEST_DIR = os.path.join(BASE_DIR, "test_dir")
# print(TEST_DIR)
#测试报告目录
REPORT_DIR = os.path.join(BASE_DIR, "test_report")


if __name__ == '__main__':
    report_date = time.strftime("%Y-%m-%d_%H_%M_%S")
    report_file = os.path.join(REPORT_DIR, str(report_date) + "result.html")
    pytest.main([
        "-n", "3", #设置线程数
        "-v", "-s", TEST_DIR,
        "--html=" + report_file,
    ])

 

标签:__,os,unittest,pytest,path,多线程,dr,self,DIR
来源: https://www.cnblogs.com/littleyang/p/15669923.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有