Selenium2+python Automation 52-unittest Execution order "reprint"

Source: Internet
Author: User

Objective

Many beginners use the unittest framework, it is unclear how the use case execution order. The classes and methods within the test class are not clear, do not know when to execute, and when not to execute.

This article explains in detail the unittest execution sequence through the simplest case.

First, case analysis

1. First define a test class with a few simple case

# Coding:utf-8
Import UnitTest
Import time
Class Test (UnitTest. TestCase):
def setUp (self):
Print "start!"

def tearDown (self):
Time.sleep (1)
Print "end!"

def test01 (self):
Print "Execute test Case 01"

def test03 (self):
Print "Execute test case 03"

def test02 (self):
Print "Execute test case 02"

def addtest (self):
Print "Add Method"

if __name__ = = "__main__":
Unittest.main ()

II. Results of implementation

D:\test\python2\python.exe d:/test/test01.py
start!
Execute test Case 01
. end!
start!
Execute test Case 02
end!
. start!
Execute test Case 03
end!
.
----------------------------------------------------------------------
Ran 3 Tests in 3.001s

Ok

Third, the result analysis

1. Order of execution:

start!-executing the test case 01-end!

start!-executing the test case 02-end!

start!-executing the test case 03-end!

2. A few points can be seen from the implementation results

--the predecessor Setup executed first, then the use case (test*) executed, and the last executed post teardown

--The execution order of test cases (test*) is performed according to 01-02-03, that is, according to the use case name to execute sequentially

--addtest (self) This method does not execute, stating that only the use case at the beginning of test

Iv. Examples of Selenium

1. Specific examples refer to this Selenium2+python Automation 48-Login method (parametric)

# Coding:utf-8
From selenium import Webdriver
Import UnitTest
Import time
Class Bolg (UnitTest. TestCase):
U ' Login blog '
def setUp (self):
Self.driver = Webdriver. Firefox ()
url = "Https://passport.cnblogs.com/user/signin"
Self.driver.get (URL)
Self.driver.implicitly_wait (30)

def login (self, username, PSW):
U ' ' here wrote a login method, account number and password parameterization '
self.driver.find_element_by_id ("Input1"). Send_keys (username)
self.driver.find_element_by_id ("Input2"). Send_keys (PSW)
self.driver.find_element_by_id ("Signin"). Click ()
Time.sleep (3)

def is_login_sucess (self):
U "" to determine whether to get to the login account name '
Try
Text = self.driver.find_element_by_id ("Lnk_current_user"). Text
Print text
Return True
Except
Return False

def test_01 (self):
U "Login Case Reference: Account number, password set"
Self.login (U "Shanghai-Yo", U "xxxx") # Invoke Login method
# Judging Results
result = Self.is_login_sucess ()
Self.asserttrue (Result)

def test_02 (self):
U "Login Case Reference: Account number, password set"
Self.login (U "Shanghai-Yo", U "xxxx") # Invoke Login method
# judgment Result # QQ Group: 232607095
result = Self.is_login_sucess ()
Self.asserttrue (Result)

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

if __name__ = = "__main__":
Unittest.main ()

Selenium2+python Automation 52-unittest Execution order "reprint"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.