When running a test case, it is necessary to judge whether the test case is executed successfully. At this time, we need to have a result we expect to verify. In unittest here, if an error is reported during the execution of a case, or our judgment result does not meet expectations, it will be judged that the execution of this use case has failed. The judgment condition is mainly realized based on assertions. This section mainly learns the use of assertions.
1. The method of assertion
1.1 Some assertion methods in the TestCase class
Judging whether the condition is established
assert_(expr,msg)
- expr: Judgment condition, return Boolean value
- msg: the message displayed after the judgment fails
Determine whether two data values are the same
assertEqual(first,second,msg)
- first: the first data
- second: the second data
- msg: the message displayed after the judgment fails
Determine whether the values of two data are different
assertEqual(first,second,msg)
- first: the first data
- second: the second data
- msg: the message displayed after the judgment fails
Determine whether the condition is true
assertTrue(expr,msg)
- expr: judgment condition
- msg: the message displayed after the judgment fails
Check if the condition is false
assertFalse(expr,msg)
- expr: judgment condition
- msg: the message displayed after the judgment fails
Determine whether two data are the same
assertIs(first,second,msg)
- first: the first data
- second: the second data
- msg: the message displayed after the judgment fails
Determine whether two data are different
assertIsNot(first,second,msg)
- first: the first data
- second: the second data
- msg: the message displayed after the judgment fails
Determine whether the data exists
assertIsNone(obj,msg)
- obj: the transmitted data
- msg: the message displayed after the judgment fails
Determine whether the data does not exist
assertIsNotNone(obj,msg)
- obj: the transmitted data
- msg: the message displayed after the judgment fails
Determine whether data a is contained in data b
assertIn(a,b,msg)
- a: data a
- b: data b
- msg: the message displayed after the judgment fails
Determine whether data a is not included in data b
assertNotIn(a,b,msg)
- a: data a
- b: data b
- msg: the message displayed after the judgment fails
1.2 Directly use assert assertion
Get used to directly using the assert assertion in python instead of the assertion method in unittest.TestCase:
Specifically, you can use it directly:
assert a == b
assert a != b
assert a in b
assert a not in b
assert a in None
assert a in not None
2. Use assertions in the framework
Since each class inherits unittest.TestCase when using unittest, when using it, use self directly in the method to call the assertion method.
import unittest
class Demo(unittest.TestCase):
def test_01(self):
self.assertEqual("demo","demo","两者不相等")
If you use assert assertion directly, you can call it directly.
import unittest
class Demo(unittest.TestCase):
def test_01(self):
assert "demo" == "demo"
3. Example demonstration
Here is an example of Baidu Tieba. The first case is to open a new page, and then judge whether the title of the new page is what we expect. The second case is that we deliberately judge a wrong title, which makes the assertion fail. These two cases Also use the above two assertion methods separately.
# -*- coding:utf-8 -*-
from selenium import webdriver
import unittest
from time import sleep
class PcLogin(unittest.TestCase):
@classmethod
def setUpClass(cls):
print("所有测试开始")
def setUp(self):
print("测试开始")
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(30)
self.driver.set_page_load_timeout(30)
self.driver.get("https://tieba.baidu.com/")
def test_01_demo(self):
"""打开新页签"""
self.driver.find_element_by_css_selector("a[title = '娱乐明星']").click()
all = self.driver.window_handles
self.driver.switch_to.window(all[1])
sleep(5)
assert self.driver.title == "娱乐明星_百度贴吧", "断言失败,当前页面title是%s " % self.driver.title
def test_02_demo2(self):
"""判断首页标题"""
# 这个例子会断言失败
self.assertEqual(self.driver.title, "娱乐明星_百度贴吧")
def tearDown(self):
print("测试完成")
self.driver.close()
@classmethod
def tearDownClass(cls):
print("所有测试结束")
if __name__ == '__main__':
unittest.main()
Running results: (two use cases were run, one of which failed; in line with our expected results)
所有测试开始
测试开始
测试完成
.测试开始
测试完成
F
======================================================================
FAIL: test_02_demo2 (__main__.PcLogin)
判断首页标题
所有测试结束
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:/1git/uitest/test_case/demo.py", line 30, in test_02_demo2
self.assertEqual(self.driver.title, "娱乐明星_百度贴吧")
AssertionError: '百度贴吧——全球最大的中文社区' != '娱乐明星_百度贴吧'
- 百度贴吧——全球最大的中文社区
+ 娱乐明星_百度贴吧
----------------------------------------------------------------------
Ran 2 tests in 35.273s
FAILED (failures=1)
Process finished with exit code 1
↵
The following is the supporting information. For friends who do [software testing], it should be the most comprehensive and complete preparation warehouse. This warehouse also accompanied me through the most difficult journey. I hope it can help you too!
Software testing interview applet
The software test question bank maxed out by millions of people! ! ! Who is who knows! ! ! The most comprehensive quiz mini program on the whole network, you can use your mobile phone to do the quizzes, on the subway or on the bus, roll it up!
The following interview question sections are covered:
1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux
6. web, app, interface automation, 7. performance testing, 8. programming basics, 9. hr interview questions, 10. open test questions, 11. security testing, 12. computer basics
method of obtaining: