Introduction to unittest automated test assertion method

Assertion is like in a test case, similar to whether the expected result is consistent with the actual result. If they are consistent, it means that the test has passed. Assert assertion is very good for test result judgment, and it is more flexible to compare the expected result and the actual result. The following is simple Let’s introduce the Assert assertion of unittest


The basic assert assertions in unittest include:

method usage illustrate
assertEqual(a, b) a == b Used to determine whether a and b are equal. If they are equal, the test passes.
assertNotEqual(a, b) a != b Used to determine if a and b are not equal. If they are not equal, the test passes.
assertTrue(x) bool(x) is True Used to determine whether the condition is true. If the condition is true, the test passes (Note: It is best to use the assertEqual assertion method instead)
assertFalse(x) bool(x) is False Used to determine whether the condition is false. If the condition is false, the test passes (Note: It is best to use the assertNotEqual assertion method instead)
assertIs(a, b) a is b Used to determine whether a and b are the same object. If so, the test passes.
assertIsNot(a, b) a is not b Used to determine whether a and b are the same object. If not, the test passes.
assertIsNone(x) x is None Used to determine whether a is empty. If it is empty, the test passes.
assertIsNotNone(x) x is not None Used to determine whether a is empty. If it is not empty, the test passes.
assertIn(a, b) a in b Used to determine whether string a appears in character b. If it appears, the test passes.
assertNotIn(a, b) a not in b Used to determine whether string a appears in character b. If it does not appear, the test passes.
assertIsInstance(a, b) isinstance(a, b) Used to determine whether a belongs to a certain data type. If it does, the test passes.
assertNotIsInstance(a, b) not isinstance(a, b) Used to determine whether a belongs to a certain data type. If it does not, the test passes.
# -*- coding:utf-8 -*-
import unittest
class MyAssertTest(unittest.TestCase):

	@classmethod
	def setUpClass(self):
		pass

	def setUp(self):
		pass

	def tearDown(self):
		pass
	@classmethod
	def tearDownClass(self):
		pass


	def test_assertNotEqual(self):
		a = 1
		b = 2
		self.assertNotEqual(a,b,msg="测试不通过,a是等于b的") #a不等于b是,测试通过

	def test_assertTrue(self):
		bool=True
		self.assertTrue(bool,msg="测试不通过,bool值为假")

	def test_assertFalse(self):
		bool=False
		self.assertFalse(bool,msg="测试不通过,bool值为真")

	def test_assertIs(self):
		a = [1]
		b = a
		self.assertIs(a,b,msg="测试不通过,a和b不是同一个obj")

	def test_assertNotIs(self):
		a = [1]
		b = [1]
		self.assertIsNot(a,b,msg="测试不通过,a和b是同一个obj")

	def test_assertIsNone(self):
		a = None
		self.assertIsNone(a,msg="测试不通过,a变量不为空")
	def test_assertIsNotNone(self):
		a = 1
		self.assertIsNotNone(a,msg="测试不通过,a变量为空")

	def test_assertIn(self):
		a = "Hello"
		str = 'Hello World'
		self.assertIn(a,str,msg="测试不通过,%s字符串不在%s中"%(a,str))

	def test_assertNotIn(self):
		a = "hello"
		str = 'Hello World'
		self.assertNotIn(a,str,msg="测试不通过,%s字符串在%s中"%(a,str))

	def test_assertIsInstance(self):
		a = 1
		self.assertIsInstance(a,str,msg="测试不通过,%s不是%s类型,%s是%s类型"%(a,str,a,type(a)))
		

	def test_assertIsNotInstance(self):
		a = 1
		self.assertNotIsInstance(a,str,msg="测试不通过")

 Summarize:

Thank you to everyone who reads my article carefully! ! !

I have personally compiled some technical materials that I have compiled in my software testing career over the past few years, including: e-books, resume modules, various work templates, interview guides, self-study projects, etc. Everyone is welcome to click on the business card below to get it for free, don’t miss it.

   Python automated testing learning exchange group: a full set of automated testing, interview, resume and learning materials. Click the link to join the group chat [Python automated testing exchange]: http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=DhOSZDNS -qzT5QKbFQMsfJ7DsrFfKpOF&authKey=eBt%2BF%2FBK81lVLcsLKaFqnvDAVA8IdNsGC7J0YV73w8V%2FJpdbby66r7vJ1rsPIifg&noverify=0&group_code=198408628

 

 

Guess you like

Origin blog.csdn.net/MXB_1220/article/details/131860293