1. 程式人生 > 其它 >登入和退出,測試用例,分別模組化,並引用測試報告和自動識別測試檔案執行(pytest升級)

登入和退出,測試用例,分別模組化,並引用測試報告和自動識別測試檔案執行(pytest升級)

技術標籤:python+seleniumseleniumpython

test/public :是登入和退出模組, 注意:_init_.py 是必須要的
test:test_cheshi2,,,test_cheshi3 這兩個分別是不同的測試用例
all_case 執行所有用例

本次用例功能點:登入,退出,用例,分別模組化,總的檔案來執行所有的用例,登入和退出是公共模組,所有放在一個資料夾,
後續用例需要就直接呼叫即可,
all_case : 在unittest的基礎上,引用了pytest框架,注意下文的第二點,如果要生成測試報告的話:只能通過cmd命令裡執行。進入這個專案下,
然後輸出;pytest --html=report.html 比如當前:cd C:\Users\Admin\PycharmProjects\測試模組 pytest --html=report.html

1.unittest提供了test cases、test suites、test fixtures、test runner相關的類,讓測試更加明確、方便、可控。使用unittest編寫用例,必須遵守以下規則:
(1)測試檔案必須先import unittest
(2)測試類必須繼承unittest.TestCase
(3)測試方法必須以“test_”開頭
(4)測試類必須要有unittest.main()方法

2.pytest是python的第三方測試框架,是基於unittest的擴充套件框架,比unittest更簡潔,更高效。使用pytest編寫用例,必須遵守以下規則:
(1)測試檔名必須以“test_”開頭或者"_test"結尾(如:test_ab.py)

(2)測試方法必須以“test_”開頭。
(3)測試類命名以"Test"開頭

all_case.py

#coding=utf-8
import unittest
#這裡需要匯入測試檔案
import sys
sys.path.append("\test")
import sys
sys.path.append('\report')
from test import *
import pytest

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

test_ceshi2.py

#coding=utf-8
import time
import unittest
import sys
sys.path.append("\\public")
#匯入登入,退出模組
from test.public import test_login
from test.public import test_exit
from selenium import webdriver

#測試用例1
class Test_cheshi2():
    #登入
    def test_cheshi2(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.driver.base_url = '自己的網址'
        print('ctt')
        driver = self.driver
        driver.get(driver.base_url)
        # 視窗最大化
        self.driver.maximize_window()

        #呼叫登入模組
        test_login.login(self)

        # 新功能引導
        # 通過兩層frame方式定位
        driver.switch_to.frame("id_iframe")
        driver.find_element_by_xpath('//*[@id="jpwClose"]').click()
        time.sleep(5)

        # 進入實驗室管理系統  吸取的教訓2:始終定位不成功的時候可以藉助火狐瀏覽器的外掛,比如:katalon 定位一遍,複製定位到的元素
        driver.find_element_by_xpath("(//div[@id='MENU_SYSGL']/span[2])[2]").click()

        time.sleep(2)
        # 退出兩層frame方式定位
        driver.switch_to.default_content()

        #呼叫退出模組
        test_exit.tearDown(self)

test_cheshi3.py

#coding=utf-8
import time
import unittest
import sys
sys.path.append("\\public")
#匯入登入,退出模組
from test.public import test_login
from test.public import test_exit
from selenium import webdriver

#測試用例2
class Test_cheshi3():
    #登入
    def test_cheshi3(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.driver.base_url = '自己的網址'
        driver = self.driver
        driver.get(driver.base_url)
        # 視窗最大化
        self.driver.maximize_window()

        #呼叫登入模組
        test_login.login(self)

        # 新功能引導
        # 通過兩層frame方式定位
        driver.switch_to.frame("id_iframe")
        driver.find_element_by_xpath('//*[@id="jpwClose"]').click()
        time.sleep(5)

        # 進入基礎管理  吸取的教訓2:始終定位不成功的時候可以藉助火狐瀏覽器的外掛,比如:katalon 定位一遍,複製定位到的元素
        driver.find_element_by_xpath('/html/body/div[3]/div[4]/div/div[1]/div[2]/span[2]').click()

        time.sleep(2)
        # 退出兩層frame方式定位
        driver.switch_to.default_content()

        #呼叫退出模組
        test_exit.tearDown(self)

test_exit.py

#coding=utf-8
from selenium import webdriver

#退出模組
def tearDown(self):
    self.driver.quit()

test_login.py

#coding=utf-8
from selenium import webdriver
import time

#登入模組
def login(self):
    driver = self.driver
    print('ctt')
    driver.find_element_by_id('showAccount').click()
    driver.find_element_by_id('userAccount').send_keys('ctt')
    driver.find_element_by_id('showPassword').click()
    driver.find_element_by_id('userPassword').send_keys('123456')
    driver.find_element_by_id('dengluBtn').click()
    time.sleep(5)

__init__.py 路徑在test\__init__.py

import test.test_cheshi2
import test.test_cheshi3

目錄結構