Python Instance Authoring (5)-Exception handling, screenshots, use case design

Source: Internet
Author: User

I. Python exception handling

Exception throw handling mechanism:

1. If an exception occurs at run time, the interpreter will find the appropriate processing statement (handler)

2. If the current function cannot be found, pass the exception to the upper calling function to see if it can handle

3. If the outermost (global "main") still cannot be found, the interpreter exits and prints out the traceback to let the user find out the cause of the error

try...except abnormal capture, with except received this Ioerror,pass representative realized the corresponding realization, do nothing

#Coding=utf-8

# except receive errors, do not error, pass represents the realization of the corresponding implementation, do nothing
Try: Open ("Ddd.txt","R")exceptIOError:Pass#msg用来打印except接收的NameError的错误, this will print out: name is not defined!Try: PrintAAexceptnameerror,msg:Printmsg #指定AssertionError的抛出错误为msg, if there is an error, quote: "num is not!"Try: Num=input ("inout a num:") assert(num==10),"num is not ten"exceptassertionerror,msg:PrintmsgPrint("Sadly,num not 10!!")

try...finally Regardless of whether the error occurred, the code must run, such as: File Close, release the lock, return the database connection to the connection pool

Import TimeTry: F=file ("Data.txt")     whileTrue:line=F.readline ()ifLen (line) = =0: BreakTime.sleep (2)        Print Linefinally: F.close ()Print 'cleaning up...closed The file'

Under normal conditions the results are as follows:

>>> What are you talking about? Seleniumgo diewfuappiumcleaning up...closed the file

Press CTRL + C to terminate the key while running, and the file still executes a finally statement:

>>>
What are you talking about? SeleniumCleaning up...closed the fileTraceback (most recent call last ):"C:\Users\ dell\desktop\test.py" in <module> time.sleep (2) keyboardinterrupt

Two. Error

Use Get_screenshot_as_file ("Saved path") in exception throws to get an exception

#Coding =utf-8 fromSeleniumImportWebdriverbrowser=Webdriver. Chrome () Browser.get ("http://www.baidu.com")Try: browser.find_element_by_id ("KW1"). SendKeys ("Selenium") browser.find_element_by_id ("su"). Click ()except: Browser.get_screenshot_as_file ("D:/error.png")    

Three. Automated test case Design

Principles of automated test case design:

1. A script is a complete scene, including login, exit and shutdown.

2. A script only verifies a single function point , such as the login function after the script is finished, verify that the message is sent while logging out, but only to verify the message this function point, not too much

3. as far as possible only to do positive logic verification , how can correctly walk through how to, consider too many reverse, on the one hand, too many scenes, on the other hand, the verification is not strong, the general manual multi-use reverse thinking, and because more for regression testing, consider positive to good

4. The dependencies between scripts cannot be too strong to be independent of each other

5. If the data is modified, to restore the data , for example: After modifying the password, you need to change the password back, otherwise it will not be used next time, reusability is too weak

6. Verify only the verification point , not every verification

Automated test cases are generally documented in tables

It is important to verify the design of the point, such as: Using the login nickname to verify the success of the login, the number of files to determine whether to add a successful file (without recovering data), the number of files to determine whether to delete the success (remember to restore the data!!!) )

For example: For the company's system management platform:

Login: Verify the nickname after login

Add File: Verify that the first column header in the first row of the newly inserted item is new

Delete file: Verify that the title of the following line is displayed correctly after deletion

Modified: The modified content is displayed correctly

Query: Whether the number of displayed content is correct

From simple to complex, gradually increase the function point, even if the implementation of some automation, but semi-automatic can make more attention to exploratory testing and user experience.

Python Instance Authoring (5)-Exception handling, use case design

Related Article

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.