当前位置:网站首页>软件测试(测试用例)编写之俗手、本手、妙手
软件测试(测试用例)编写之俗手、本手、妙手
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篇面试经验文章和项目案例源码、笔记等等资料。具体详细内容可自行查看哦!

边栏推荐
- 项目组织战略管理
- 使用ML.NET+ONNX预训练模型整活B站经典《华强买瓜》
- sqlmap基本使用方法
- Small file special
- Flink <-->JDBC的使用介绍+with参数
- MySQL -- index principle + how to use
- Unity学习笔记:联网游戏Pixel Adventure 1学习过程&纠错心得
- A detailed explanation of vector derivative and matrix derivative
- The story of a 30-year-old tester struggling, even lying flat is extravagant
- Jupiter notebook changing font style and font size
猜你喜欢

UI interface design related knowledge (I)

QT:QSS自定义QTableView实例

那些一門心思研究自動化測試的人,後來怎樣了?

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

Is it OK to test the zero basis software?

多路IO转接——前导

Introduction to deep learning linear algebra (pytorch)

Set ArrayList nested map set loop traversal

Automatic derivation of introduction to deep learning (pytoch)

Unity learning notes: online game pixel Adventure 1 learning process & error correction experience
随机推荐
[combinatorial mathematics] pigeon's nest principle (simple form of pigeon's nest principle | simple form examples of pigeon's nest principle 1, 2, 3)
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
Leetcode skimming ---75
Entropy method to calculate weight
Unity group engineering practice project "the strongest takeaway" planning case & error correction document
Leetcode skimming ---217
[untitled]
[roast & brain hole] Some Thoughts on the bullet screen interactive game of Wei Shu Wu Three Kingdoms when visiting station B
Buy health products for parents
现在零基础转行软件测试还OK吗?
Linear regression of introduction to deep learning (pytorch)
那些一门心思研究自动化测试的人,后来怎样了?
Unity learning notes: personal learning project "crazy genius Edgar" error correction document
Leetcode skimming ---832
MySQL -- index principle + how to use
带你走进云原生数据库界扛把子Amazon Aurora
Flink < --> how to use redis +with parameter
QT:QSS自定义 QStatusBar实例
UI interface design related knowledge (I)
Jupiter notebook changing font style and font size