python3 selenium自动化 unittest单元测试 百度搜索例子详解

unittest单元测试框架
分为4个部分
1.预置条件
2.环境恢复
3.用例执行部分
4.测试结果
以下我来讲百度搜索的例子,分别搜索shawn,james,xiezhiming。执行三个用例。以下为详细的代码:

import unittest   #调用unittest模块

from selenium import webdriver
from time import  sleep


class test(unittest.TestCase):

'''预置条件'''
    @classmethod
    def setUpClass(cls):
        print('用例执行前----------->')

'''环境恢复'''
    @classmethod
    def tearDownClass(cls):
        print('用例执行后----------->')

'''执行第一条用例,搜索shawn'''
    def test_baidu1(self):
        self.dr=webdriver.Chrome()
        baidu=self.dr
        baidu.get('https://www.baidu.com/')
        sleep(3)
        baidu.maximize_window()
        sleep(2)
        baidu.find_element_by_id('kw').send_keys('shawn')
        baidu.find_element_by_id('su').click()
        sleep(3)
        title1=baidu.title
        '''断言 shawn_百度搜索'''
        self.assertEqual('shawn_百度搜索',title1)
        baidu.quit()
        print('用例1执行完毕')

'''执行第2条用例,搜索james'''
    def test_baidu2(self):
        self.dr=webdriver.Chrome()
        baidu=self.dr
        baidu.get('https://www.baidu.com/')
        sleep(3)
        baidu.maximize_window()
        sleep(2)
        baidu.find_element_by_id('kw').send_keys('james')
        baidu.find_element_by_id('su').click()
        sleep(3)
        title1=baidu.title
        '''断言 james_百度搜索'''
        self.assertEqual('james_百度搜索',title1)
        baidu.quit()
        print('用例2执行完毕')

'''执行第3条用例,搜索xiezhiming'''
    def test_baidu3(self):
        self.dr=webdriver.Chrome()
        baidu=self.dr
        baidu.get('https://www.baidu.com/')
        sleep(3)
        baidu.maximize_window()
        sleep(2)
        baidu.find_element_by_id('kw').send_keys('xiezhiming')
        baidu.find_element_by_id('su').click()
        sleep(3)
        title1 = baidu.title
        '''断言 xiezhiming_百度索,故意断言错误,看程序执行情况'''
        self.assertEqual('xiezhiming_百度索', title1)
        baidu.quit()
        print('用例3执行完毕')


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

测试结果如下:

Connected to pydev debugger (build 181.5087.37)
Launching unittests with arguments python -m unittest test.test in E:\untitled2\testcases
用例执行前----------->用例1执行完毕
用例2执行完毕


Ran 3 tests in 55.536s

FAILED (failures=1)


xiezhiming_百度搜索 != xiezhiming_百度索

Expected :xiezhiming_百度索
Actual   :xiezhiming_百度搜索
 <Click to see difference>

Traceback (most recent call last):
  File "D:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pycharm\teamcity\diff_tools.py", line 32, in _patched_equals
    old(self, first, second, msg)
  File "D:\Program Files\Python37\lib\unittest\case.py", line 839, in assertEqual
    assertion_func(first, second, msg=msg)
  File "D:\Program Files\Python37\lib\unittest\case.py", line 1220, in assertMultiLineEqual
    self.fail(self._formatMessage(msg, standardMsg))
  File "D:\Program Files\Python37\lib\unittest\case.py", line 680, in fail
    raise self.failureException(msg)
AssertionError: 'xiezhiming_百度索' != 'xiezhiming_百度搜索'
- xiezhiming_百度索
+ xiezhiming_百度搜索
?              +


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Program Files\Python37\lib\unittest\case.py", line 59, in testPartExecutor
    yield
  File "D:\Program Files\Python37\lib\unittest\case.py", line 615, in run
    testMethod()
  File "E:\untitled2\testcases\test.py", line 57, in test_baidu3
    self.assertEqual('xiezhiming_百度索', title1)

用例执行后----------->
Process finished with exit code 1

猜你喜欢

转载自blog.csdn.net/xiezhiming1234/article/details/82152947