unittest测试框架搭建_开源自动化测试框架

2022-09-27 15:15:01 浏览数 (1)

本文采用unittest测试框架搭建一个简单的自动化测试实例,完成readmine网站的登录和新建项目用例,并生成测试报告。

test_login文件

代码语言:javascript复制
import unittest
from selenium import  webdriver
class TestStorm(unittest.TestCase):
    def setUp(self):
        self.driver=webdriver.Chrome()
        self.driver.get("http://demo.redmine.org/")
        self.driver.maximize_window()
        self.driver.implicitly_wait(30)

    def test_wrongpw(self):
        """ 正确的用户名错误的密码"""

        self.driver.find_element_by_xpath('//*[@id="account"]/ul/li[1]/a').click()
        self.driver.find_element_by_id('username').clear()
        self.driver.find_element_by_id('username').send_keys('XXXXXXXXx')
        self.driver.find_element_by_id('password').clear()
        self.driver.find_element_by_id('password').send_keys('XXXXXXXXXXXXXXXxx')
        self.driver.find_element_by_name('login').click()
        errormessage=self.driver.find_element_by_xpath('//*[@id="flash_error"]').text
        self.assertEqual(errormessage,'无效的用户名或密码')
    def test_successlogin(self):
        """ 正确的用户名正确的密码"""

        self.driver.find_element_by_xpath('//*[@id="account"]/ul/li[1]/a').click()
        self.driver.find_element_by_id('username').clear()
        self.driver.find_element_by_id('username').send_keys('XXXXXXXXXXXXXXx')
        self.driver.find_element_by_id('password').clear()
        self.driver.find_element_by_id('password').send_keys('XXXXXXXXXXXXXXXX')
        self.driver.find_element_by_name('login').click()
        idaccount=self.driver.find_element_by_xpath('//*[@id="loggedas"]/a').text
        self.assertEqual(idaccount,'yangyali')

    def tearDown(self):
        self.driver.quit()



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

test_new_project文件

代码语言:javascript复制
import unittest
from selenium import  webdriver
class TestStorm(unittest.TestCase):
    def setUp(self):
        self.driver=webdriver.Chrome()
        self.driver.get("http://demo.redmine.org/")
        self.driver.maximize_window()
        self.driver.implicitly_wait(20)
        self.driver.find_element_by_xpath('//*[@id="account"]/ul/li[1]/a').click()
        self.driver.find_element_by_id('username').clear()
        self.driver.find_element_by_id('username').send_keys('XXXXXXXXXXXXxx')
        self.driver.find_element_by_id('password').clear()
        self.driver.find_element_by_id('password').send_keys('XXXXXXXXXXXXXx')
        self.driver.find_element_by_name('login').click()

    def test_xinjianxiangmu(self):
        """ 创建项目成功"""

        self.driver.find_element_by_xpath('//*[@id="top-menu"]/ul/li[3]/a').click()
        self.driver.find_element_by_xpath('//*[@id="content"]/div[1]/a[1]').click()
        self.driver.find_element_by_id('project_name').send_keys('project2')
        self.driver.find_element_by_xpath('//*[@id="new_project"]/input[3]').click()
        successmessage=self.driver.find_element_by_xpath('//*[@id="flash_notice"]').text
        self.assertEqual(successmessage,'创建成功')
        self.assertEqual(self.driver.find_element_by_xpath('//*[@id="header"]/h1').text,'project2')


    def tearDown(self):
        self.driver.quit()



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

run文件,用来执行用例并生成报告

代码语言:javascript复制
import unittest
import HTMLTestRunner
import time,os
if __name__ == '__main__':
    #查找当前目录的测试用例文件
    testSuite=unittest.TestLoader().discover('.')
    filename=os.getcwd() os.sep "storm_{}.html".format(time.strftime('%Y%m%d%H%M%S',time.localtime(time.time())))
    with open(filename,'wb')as f:
        runner=HTMLTestRunner.HTMLTestRunner(stream=f,title='Readmine测试报告',description='unittest线性测试报告')
        runner.run(testSuite)

生成的测试报告截图:

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/183747.html原文链接:https://javaforall.cn

0 人点赞