2.1pycharm目录如图
目录.png
代码.png
例子:test_login.py
import unittest
from selenium import webdriver
import time
from write_read_cookie.skip_login import save_cookie
class Test(unittest.TestCase):
@classmethod
def setUpClass(cls):
# print('开始测试,打开浏览器')
cls.driver = webdriver.Chrome()
cls.url="http://dev.dingnuo.ai:3000/login"
@classmethoddef tearDownClass(cls): # print('结束测试,关闭浏览器') cls.driver.close() cls.driver.quit()def setUp(self): if self.driver.current_url not in self.url: self.driver.get(self.url) time.sleep(2)def login(self, username, password): self.driver.find_element_by_id('login-username').clear() self.driver.find_element_by_id('login-username').send_keys(username) self.driver.find_element_by_id('login-password').clear() self.driver.find_element_by_id('login-password').send_keys(password) self.driver.find_element_by_link_text(u'登录').click() time.sleep(5) return self.driver.current_url# 用户名,密码正确def test_login_success(self): current_url = self.login('jindaqian', '123456') self.assertEqual('http://dev.dingnuo.ai:3000/questionList', current_url) ##将登录后的cookie保存到本地文件中,下次其他页面操作,通过cookie跳过登录 save_cookie(self.driver)# 正确用户名,密码错误def test_login_pwd_error(self): current_url = self.login('jindaqian', '12345') self.assertEqual('http://dev.dingnuo.ai:3000/login', current_url) self.driver.get_screenshot_as_file("screenshot/login_pwd_error.jpg")# 用户名错误,密码正确def test_login_uname_error(self): current_url = self.login('jdq', '123456') self.assertEqual('http://dev.dingnuo.ai:3000/login', current_url) self.driver.get_screenshot_as_file("screenshot/login_uname_error.jpg")# 用户名密码都是空的情况下直接点击登录def test_login_uname_pwd_empty(self): current_url = self.login('','') self.assertEqual('http://dev.dingnuo.ai:3000/login', current_url) self.driver.get_screenshot_as_file("screenshot/login_uname_pwd_empty.jpg")
if name == 'main':
unittest.main()
例子:run_all_case.py
import unittest
import os
import HTMLTestRunner
def allcase():
current_path = '/工作文件/workspace/dingnuo/case'
testsuite = unittest.TestSuite()
discover = unittest.defaultTestLoader.discover(current_path,pattern='test*.py',top_level_dir=None)
# discover方法筛选出来的用例,循环添加到测试套件中
for test_suite in discover:
for test_case in test_suite:
print(test_case)
#添加测试用例到testcase中
testsuite.addTest(test_case)
return testsuite
if name=='main':
fp=open('report.html',"wb")
runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title='问答社区管理后台测试报告',
description='用例执行情况:')
runner.run(allcase())
fp.close()
运行结果
1533692992105.jpg
作者:不撸代码不舒服斯基
链接:https://www.jianshu.com/p/aab2a2845cbe
共同学习,写下你的评论
评论加载中...
作者其他优质文章