当前位置:网站首页>软件测试(测试用例)编写之俗手、本手、妙手
软件测试(测试用例)编写之俗手、本手、妙手
2022-07-03 09:41:00 【入坑玩家】
今年高考作文火出圈了。
“妙手、俗手、本手” 本为围棋里面的概念。
本手:是指合乎棋理的正规下法。妙手: 是指出人意料的精妙下法。俗手:是指貌似合理,实际上是错误的。
当然,我不会下围棋,在写自动化测试的过程中,我经常遇到俗手、本手、妙手的写法。接下来,就和大家探讨一下。
俗手写法
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("报错信息: %s" %msg)
finally:
dr.quit()
if __name__ == '__main__':
unittest.main()
我经常看到有测试在写用例的时候喜欢用 try...except... , 上面的代码,把可能定位不到的元素操作try起来,万一出错了就捕捉到错误信息,最终 finally 还对打开的浏览器进行了关闭处理。看似很合理对吧!?
假设,修改元素定位,报错信息如下:
报错信息:Message: no such element: Unable to locate element: {
"method":"css selector","selector":"[id="sb_form_q11"]"}
.
----------------------------------------------------------------------
Ran 1 test in 4.914s
OK
请问:这条用例成功了还是失败了?如果说成功了,明明打印了“报错信息”;如果说失败了,结果却显示“Ok”。
本手写法
# ...
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()
去掉不必要的异常捕捉,合理地使用setUp/tearDown,再次运行测试。
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)
报错信息很吓人吗?丰富的报错信息有助于你定位问题!
File ".\demo.py", line 31
哪个文件哪一个行代码。
dr.find_element(By.ID, "sb_form_q11").submit()
具体报错的代码是什么。
NoSuchElementException
异常的类型,异常类型有助于锁定问题。就像医院的医导一样,你说肚子疼,他马上知道你应该去挂哪个科室。有经验的程序员只看错误类型就知道问题出在哪里了,都不用看具体报错信息。
Message: no such element: Unable to locate element: {
"method":"css selector","selector":"[id="sb_form_q11"]"}
具体的报错信息,他会告诉你为什么出错了。
FAILED (errors=1)
用例的统计结果是对的,确实有一条用例错误。
tearDown()
tearDown() 默默地执行了浏览器关闭动作。
所以,你为什么要在用例里面加异常捕捉?你到底想隐瞒什么?是自我的欺骗?还是来自领导的压力?
妙手写法
# ...
def bing_search(dr, keyword):
"""搜索封装"""
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()
妙手的写法自然是做好合理的封装,当然这里的技巧包括不限于于 PO 设计模式,数据驱动等,以往的文章做了大量的讨论这里就不展开了。
最后,
我们时常能听到 人生如棋,工作也好,写代码也好,都会经历俗手->本手 -> 妙手, 一开始写代码,因为缺乏经验,往往从自己的认为是合理的方式编写,当然,往往容易踩坑,这即为俗手;随着经验的不断积累,总结出了一些经验就会按照常规方式编写代码,这即为本手;随着经验的不断积累,偶得一些设计模式、语法糖等,这即为妙手。
当然了,程序员不管什么手,最终的归宿都是外卖骑手。
点击下面卡片关注本博主个人公众号可以获取更多技术干货,包括自动化测试学习资料,知识体系大纲,40篇面试经验文章和项目案例源码、笔记等等资料。具体详细内容可自行查看哦!

边栏推荐
- 有些能力,是工作中学不来的,看看这篇超过90%同行
- Detailed cross validation and grid search -- sklearn implementation
- Jetson TX2 brush machine
- Install yolov3 (Anaconda)
- [combinatorial mathematics] pigeon nest principle (simple form examples of pigeon nest Principle 4 and 5)
- 【吐槽&脑洞】关于逛B站时偶然体验的弹幕互动游戏魏蜀吴三国争霸游戏的一些思考
- Leetcode skimming ---44
- Day 7 small exercise
- MySql 怎么查出符合条件的最新的数据行?
- Buy health products for parents
猜你喜欢

面试官:Redis中列表的内部实现方式是什么?

QT:QSS自定义QTableView实例

Automatic derivation of introduction to deep learning (pytoch)

带你走进云原生数据库界扛把子Amazon Aurora

What happened to those who focused on automated testing?

使用ML.NET+ONNX预训练模型整活B站经典《华强买瓜》

Hou Jie -- STL source code analysis notes

测试理论概述

Bid -- service commitment -- self summary

帶你走進雲原生數據庫界扛把子Amazon Aurora
随机推荐
C language project: student achievement system
【吐槽&脑洞】关于逛B站时偶然体验的弹幕互动游戏魏蜀吴三国争霸游戏的一些思考
软件测试——Redis数据库
How to make a blood bar in the game
Traversal of map set
Some abilities can't be learned from work. Look at this article, more than 90% of peers
我,大厂测试员,降薪50%去国企,后悔了...
Numpy quick start (I) -- pre knowledge (create array + constant + data type)
[untitled]
A detailed explanation of vector derivative and matrix derivative
Promoted, colleagues become subordinates and don't cooperate with work
Unity learning notes: personal learning project "crazy genius Edgar" error correction document
Unity小组工程实践项目《最强外卖员》策划案&纠错文档
Take you into the cloud native database industry, Amazon Aurora
MySQL -- index principle + how to use
正常一英寸25.4厘米,在影像领域是16厘米
[roast & brain hole] Some Thoughts on the bullet screen interactive game of Wei Shu Wu Three Kingdoms when visiting station B
Bidding website architecture project progress -- Network Security
Numpy quick start (III) -- array advanced operation
Leetcode skimming ---10