当前位置:网站首页>Software testing (test case) writing: vulgar, native and skillful
Software testing (test case) writing: vulgar, native and skillful
2022-07-03 10:56:00 【Pit player】
This year's college entrance examination composition is popular .
“ A good hand 、 Vulgar hand 、 My hand ” This is the concept of go .
My hand
: It refers to the normal playing method in line with chess theory .A good hand
: It refers to the unexpected subtle execution .Vulgar hand
: It means seemingly reasonable , It's actually wrong .
Of course , I can't play go , In the process of writing automated tests , I often meet Vulgar hand
、 My hand
、 A good hand
Writing . Next , Just discuss with you .
Vulgar handwriting
import time
import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
class TestSample(unittest.TestCase):
def test_case(self):
dr = webdriver.Chrome()
try:
dr.get("https://cn.bing.com/")
dr.find_element(By.ID, "sb_form_q").send_keys("selenium")
dr.find_element(By.ID, "sb_form_q11").submit()
time.sleep(2)
except BaseException as msg:
print(" Error message : %s" %msg)
finally:
dr.quit()
if __name__ == '__main__':
unittest.main()
I often see that some tests like to use when writing use cases try...except...
, The above code , Operate elements that may not be located try
get up , If something goes wrong, you will catch the error message , Final finally
The open browser is also closed . It seems reasonable, right !?
hypothesis , Modify element positioning , The error information is as follows :
Error message :Message: no such element: Unable to locate element: {
"method":"css selector","selector":"[id="sb_form_q11"]"}
.
----------------------------------------------------------------------
Ran 1 test in 4.914s
OK
Excuse me, : Does this use case succeed or fail ? If it succeeds , Clearly printed “ Error message ”; If it fails , It turns out “Ok”.
This manual writing method
# ...
class TestSample(unittest.TestCase):
def setUp(self) -> None:
self.dr = webdriver.Chrome()
def test_case(self):
dr = self.dr
dr.get("https://cn.bing.com/")
dr.find_element(By.ID, "sb_form_q").send_keys("selenium")
dr.find_element(By.ID, "sb_form_q11").submit()
time.sleep(2)
def tearDown(self) -> None:
self.dr.quit()
if __name__ == '__main__':
unittest.main()
Remove unnecessary exception catching , Use... Reasonably setUp/tearDown
, Run the test again .
E
======================================================================
ERROR: test_case (__main__.TestSample)
----------------------------------------------------------------------
Traceback (most recent call last):
File ".\demo.py", line 31, in test_case
dr.find_element(By.ID, "sb_form_q11").submit()
File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1244, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 424, in execute
self.error_handler.check_response(response)
File "C:\Python38\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {
"method":"css selector","selector":"[id="sb_form_q11"]"}
...
----------------------------------------------------------------------
Ran 1 test in 4.826s
FAILED (errors=1)
Is it frightening to report wrong information ? Rich error reporting information helps you locate the problem !
File ".\demo.py", line 31
Which file and which line of code .
dr.find_element(By.ID, "sb_form_q11").submit()
What is the specific error code .
NoSuchElementException
Type of exception , Exception types help lock problems . Just like the medical director in the hospital , You said your stomach hurts , He immediately knows which department you should go to . Experienced programmers only look at the error type to know where the problem is , Don't look at the specific error information .
Message: no such element: Unable to locate element: {
"method":"css selector","selector":"[id="sb_form_q11"]"}
Specific error reporting information , He will tell you why something went wrong .
FAILED (errors=1)
The statistical results of use cases are correct , There is indeed a use case error .
tearDown()
tearDown()
Silently executed the browser closing action .
therefore , Why do you add exception capture in use cases ? What are you trying to hide ? It's self deception ? Or the pressure from leaders ?
Wonderful handwriting
# ...
def bing_search(dr, keyword):
""" Search for encapsulation """
dr.find_element(By.ID, "sb_form_q").send_keys(keyword)
dr.find_element(By.ID, "sb_form_q11").submit()
time.sleep(2)
class TestSample(unittest.TestCase):
def setUp(self) -> None:
self.dr = webdriver.Chrome()
def test_case(self):
dr = self.dr
dr.get("https://cn.bing.com/")
bing_search(dr, "selenium")
def tearDown(self) -> None:
self.dr.quit()
if __name__ == '__main__':
unittest.main()
The writing method of skillful hand is naturally to do a reasonable package , Of course, the skills here include but are not limited to PO Design patterns
, Data driven
etc. , Previous articles have done a lot of discussion, so I won't start here .
Last ,
We often hear Life is like chess , Work is good , It's good to write code , Will experience vulgar hands -> My hand -> A good hand , Start writing code , Because of lack of experience , Often write in a way that you think is reasonable , Of course , It is often easy to step on the pit , This is called vulgar hand ; With the accumulation of experience , After summing up some experience, you will write code in the conventional way , This is my hand ; With the accumulation of experience , I got some design patterns 、 Grammar, sugar, etc , This is a wonderful hand .
Yes, of course , Programmers don't care what hand , The ultimate destination is the take away rider .
Click the card below to follow the blogger's personal official account to get more technical dry goods , Including automated test learning materials , Outline of knowledge system ,40 Interview experience articles and project case source code 、 Notes, etc . You can check the details by yourself !
边栏推荐
- 测试理论概述
- Flink <-->JDBC的使用介绍+with参数
- QT: QSS custom qtableview instance
- QT:QSS自定义QHeaderView实例
- How does MySQL find the latest data row that meets the conditions?
- User recommendation preference model based on attention enhanced knowledge perception
- Common scenarios in which Seata distributed transactions fail and do not take effect (transactions do not rollback)
- [roast & brain hole] Some Thoughts on the bullet screen interactive game of Wei Shu Wu Three Kingdoms when visiting station B
- QT:QSS自定义QToolBar和QToolBox实例
- Qt:qss custom qgroupbox instance
猜你喜欢
How to monitor the incoming and outgoing traffic of the server host?
测试理论概述
Snownlp emotion analysis
Qt:qss custom qscrollbar instance
17K薪资要什么水平?来看看95后测试工程师的面试全过程…
游戏测试相关 测试一个英雄的技能(春招被问比较多的一道题)
Differences among norm, normalize and normalized in eigen
Bid -- service commitment -- self summary
Numpy realizes the classification of iris by perceptron
8年测试总监的行业思考,看完后测试思维认知更深刻
随机推荐
If you always feel that you need to persist in learning English
Classification (data consolidation and grouping aggregation)
Use ml.net+onnx pre training model to liven the classic "Huaqiang buys melons" in station B
Qt:qss custom qmenubar instance
Flink-- custom function
Flink--自定义函数
Chiyou (), a specific mythical image, is also gradually abstracted as a dramatic character type "Jing". "Jing", born in Dan Dynasty and ugly at the end, is the earliest "profession" in Chinese drama
Numpy quick start (V) -- Linear Algebra
QT:QSS自定义QToolButton实例
Flink -- built in function (all)
“测试人”,有哪些厉害之处?
C语言项目:学生成绩系统
Qt:qss custom qlistview instance
MySql 怎么查出符合条件的最新的数据行?
Basic theoretical knowledge of software testing -- app testing
Wechat applet training notes 1
Redis notes 01: Introduction
What happened to those who focused on automated testing?
Bidding website architecture project progress -- Network Security
Qt:qss custom qradiobutton instance