python + Day unittest unittest frame assertion of (a)

unittest test in two days assertion summary, hhh, not much content, that is lazy -

What is the role of the assertion? A: After setting up a test assertions, can help us to judge the results of the test case.

We look at unittest support the assertion of which:

Have a general understanding of the above assertion syntax, we use it and see the code:

One:

Assertion assertEqual (a, b) it can determine the two parameters are equal, for example, we get to the Baidu home page title and we write the title to do comparison.
1  '' ' 
2  title asserted assertEqual () can determine which equal two parameters, such as we get to the Baidu home page title and we write the title to do comparison.
. 3  '' ' 
. 4  Import the unittest
 . 5  from bokeyuan.bokeyuan_public Import *
 . 6  
. 7  class baidu_page (public_baidu):     # inherits class public_baidu .py another module, the class inherits TestCase 
. 8      ' '' 
. 9      because of their other inherited a module class .py public_baidu, it is not necessary to write in two test setup and teardown methods firmware.
10      '' 
11      '' ' directly written in Example ' '' 
12 is      DEF test_baidu_title (Self):    # As long as cases begin with, the function name must be test_ or will not be recognized as use cases! ! 
13         Print ( ' acquired title is: ' , self.driver.title)   # we get to print the contents of title 
14          self.assertEqual (self.driver.title, ' Baidu, you know ' )    # second argument is what we need to write the check, oh 
15  
16      @staticmethod      
 17      DEF Suite (Self):
 18          unittest.TestSuite (unittest.makeSuite (baidu_page))
 19          return Suite      # be sure to return the results to the caller 
20  IF  __name__ == ' __main__ ' :
 21 is     unittest.main (the verbosity = 2) .run (baidu_page.suite ())   # the case baidu_page class, passing the test suite suite (), suite () method is decorated, so no instantiated you can directly call the class name

Note: equal, and the content must be exactly equal oh types, such as str ( '1') and int (1) These two 1 content is the same, but they are not the same type of data, so it will not be equal Oh! And string data type is a type two oh! ! ! Two equal meaning you learn it?

So assertNotEqual (a, b) representatives are not equal, you can draw inferences write small examples? Title: determine what the range 4 3  

The message can not be friends with everyone to learn from each other ~

Two: assertTrue (x) returns bool data type, True everyone is familiar with, Barbara true, true and false is false false false false ~ ~

We see a small case, I determine what the Baidu home page input box can enter content, if you can edit, then bool value is certainly true (True) ~~

Remarks:

so.is_enabled () function is to check whether an element such as text boxes can be edited
1  '' ' 
2  title asserted assertTrue () which returns Ture, i.e. the condition is true, is adopted as a true Barbara ~~
 . 3  ' '' 
. 4  Import the unittest
 . 5  from bokeyuan.bokeyuan_public Import *
 . 6  
. 7  class baidu_page ( public_baidu):     # inherits another class public_baidu .py module, this class inherits TestCase 
8      '' ' 
9      because of their own inherited another class public_baidu .py module, so no need to write setup and teardown two test methods firmware.
10      '' 
11      '' ' directly written in Example ' '' 
12 is      DEF test_baidu_ShuRuKuang (Self):    # As long as cases begin with, the function name must be test_ or will not be recognized as use cases! ! 
13          '' 'Home authentication Baidu input box can be input content '' ' 
14          SO = self.driver.find_element_by_id ( ' kW ' )
 15          self.assertTrue (so.is_enabled ())      # It so.is_enabled () function is to check whether the elements The text edit box 
16  
. 17      @staticmethod
 18 is      DEF Suite (Self):
 . 19          unittest.TestSuite (unittest.makeSuite (baidu_page))
 20 is          return Suite      # must return a result to the caller 
21 is  IF  the __name__ == ' __main__ ' :
 22 is      the unittest. main (the verbosity = 2) .run (baidu_page.suite ())   #The case baidu_page class, passing the test suite suite (), suite () method is decorated, so do not need to instantiate the class name can be directly invoked

Can see the case is ok, represented by description can be edited through case you would use assertTrue (x) yet? It is not know it?

The same assertFalse () for false, you can use it to determine what it 3 is equal to 4? If you do not write that message ohhh we will study together

Three, aeertIn (a, b) represents the a-b which is not, or is contained in b. We look at code examples:

 1 '''
 2 标题  断言 assertIn(a,b)   代表a是不是被b所包含,a是不是在b里面
 3 '''
 4 import unittest
 5 from bokeyuan.bokeyuan_public import *
 6 
 7 class baidu_page(public_baidu):    #继承了另一个.py模块中的public_baidu类,这个类继承了TestCase
 8     '''
 9     因为继承了自己另一个.py模块中的类public_baidu,所以不需要在写setup和teardown两个测试固件的方法。
10     '''
11     '''直接写用例'''
12     def test_baidu_ShuRuKuang(self):    #只要是用例,函数名字开头必须是test_   不然不会被识别成用例的!!
13         '''验证百度两个字,在百度的title内容中包含'''
14         self.assertIn('百度',self.driver.title)
15 
16     @staticmethod
17     def suite(self):
18         unittest.TestSuite(unittest.makeSuite(baidu_page))
19         return suite     #一定要返回结果给调用方
20 if __name__ == '__main__':
21     unittest.main(verbosity=2).run(baidu_page.suite())  #把baidu_page类中的案例,传入到测试套件中suite(),suite()方法是被装饰了的,所以不需要实例化可以直接类名调用

同样的方法,你能有可以用assertIsNot(a, b)  代表a不在b中包含。写一个小案例吗?题目: 字符串 ’b‘  不在 字符串 ’acc‘ 中包含

今天的内容就是这么多了,其实常用的断言(assert)方法就是这三种,其他的也会用到,但是不要贪,先学会这三种以及相反的三种,就是六种了~~棒~~~

不会的朋友,可以留言,大家一起学习~

 

 

Guess you like

Origin www.cnblogs.com/woshidaliua/p/11385560.html