1. 程式人生 > 程式設計 >Python分類測試程式碼例項彙總

Python分類測試程式碼例項彙總

1.自動化測試裡面的測試用例設計的一些方法

解耦、可以獨立執行、需要靈活切換

設計思路: 指令碼功能分析(分步驟)和模組化分層(拆分為多模組)

project

login_order.py #登入下單測試用例
category.py #選單分類測試用例

all_test.py #主入口

login_order.py

# -*- coding: UTF-8 -*-
import unittest
import time
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains


class LoginOrderTestCase(unittest.TestCase):
  def setUp(self):
    print("測試開始")
    self.driver = webdriver.Firefox()
    self.driver.implicitly_wait(20)
    self.base_url = "https://xdclass.net"
    self.driver.get(self.base_url)

  def tearDown(self):
    print("單個測試用例結束")
    pass
    #單個測試用例結束
  
  def test_login_order(self):
    u"登入測試用例"
    driver = self.driver
    #登入框
    login_ele = driver.find_element_by_css_selector("#login")
    ActionChains(driver).click(login_ele).perform()

    sleep(2)
    #查詢輸入框,輸入賬號,輸入框要提前清理裡面的資料
    driver.find_element_by_id("phone").clear()
    driver.find_element_by_id("phone").send_keys("13113777338")
    #查詢密碼輸入框,輸入密碼
    driver.find_element_by_id("pwd").clear()
    driver.find_element_by_id("pwd").send_keys("123456789")

    #拿到登入按鈕
    login_btn_ele = driver.find_element_by_css_selector("button.login")
    #觸發點選事件,登入
    login_btn_ele.click()
    #判斷登陸是否成功,邏輯-》滑鼠移到上面,判斷彈窗字元
    #獲取滑鼠上移的元素
    user_info_ele = driver.find_element_by_css_selector(".user_head_portrait")
    sleep(1)
    #hover觸發
    ActionChains(driver).move_to_element(user_info_ele).perform()
    sleep(1)
    #獲取使用者名稱稱元素
    user_name_ele = driver.find_element_by_css_selector(".img_name > span:nth-child(2)")
    print("===測試結果==")
    print(user_name_ele.text)

    name = user_name_ele.text
    #self.assertEqual(name,u"二當家小D",msg="登入失敗")

    video_ele = driver.find_element_by_css_selector("div.hotcourses:nth-child(3) > div:nth-child(2) > div:nth-child(1) > ul:nth-child(1) > li:nth-child(1) > a:nth-child(1) > div:nth-child(1) > img:nth-child(1)")
    video_ele.click()
    sleep(2)

    buy_btn_ele = driver.find_element_by_css_selector(".learn_btn > a:nth-child(1)")

    buy_btn_ele.click()
    print("進入下單頁面")
    
if __name__ == '__main__':
    unittest.main()

category.py

# -*- coding: UTF-8 -*-
import unittest
import time
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains

class CategoryTestCase(unittest.TestCase):
  def setUp(self):
    print("測試開始")
    self.driver = webdriver.Firefox()
    self.driver.implicitly_wait(20)
    self.base_url = "https://xdclass.net"
    self.driver.get(self.base_url)


  def tearDown(self):
    print("測試結束")
    #單個測試用例結束
    self.driver.quit()

  def test_menu(self):
    u"彈出選單測試用例"
    driver = self.driver
    #跳轉網頁
    sleep(1)

    #定位到滑鼠移動到上面的元素
    menu_ele = driver.find_element_by_css_selector("#banner_left_ul > a:nth-child(1) > li:nth-child(1) > span:nth-child(1)")

    #對定位到的元素執行滑鼠移動到上面的操作
    ActionChains(driver).move_to_element(menu_ele).perform()
    sleep(2)
    #選中子選單
    sub_meun_ele = driver.find_element_by_css_selector("#des > li:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > a:nth-child(1)")

    sub_meun_ele.click()
    sleep(2)


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

all_test.py

# -*- coding: UTF-8 -*-
import unittest
import HTMLTestRunner
import login_order,category
import time

#建立測試集合  
def create_suite():
  print("測試開始")
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(login_order.LoginOrderTestCase))
  suite.addTest(unittest.makeSuite(category.CategoryTestCase))
  return suite
   

if __name__ == '__main__':
  suite = create_suite()

  #檔名中加了當前時間,為了每次生成不同的測試報告
  file_prefix = time.strftime("%Y-%m-%d %H_%M_%S",time.localtime())

  #建立測試報告,此時這個檔案還是空檔案 wb 以二進位制格式開啟一個檔案,只用於寫入,如果檔案存在則覆蓋,不存在則建立
  fp = open("./"+file_prefix+"_result.html","wb")
  
  # stream定義一個測試報告寫入的檔案,title就是標題,description就是描述
  runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title=u"小D課堂 測試報告",description=u"測試用例執行情況",verbosity=2)
  runner.run(suite)
  fp.close()

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。