当前位置:首页 >> 脚本专栏

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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。