测试开发技术网站
博客
设计
设计
开发
Python
测试
unittest
运维
Linux基础应用
CI/CD
CI/CD
数据库
数据库
云计算
云计算
云原生
云原生
爬虫
爬虫
数据分析
数据分析
人工智能
人工智能
登录
注册
Pytest执行脚本
收藏本文
作者:redrose2100 类别: 日期:2022-05-13 15:51:56 阅读:859 次 消耗积分:0 分
## 一、执行已安装包中的测试用例 在某一些场景下,比如需要执行发布包中的测试用例或者自己开发了一个包发布安装之后想再执行已安装包的测试用例(当然发布包的时候需要将测试文件夹一起打包,否则找不到用例),此时在任意位置使用 --pyargs参数指定即可,比如如下指定执行numpy包的tests用例执行 ```bash $ pytest --pyargs numpy.tests ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog\tests\demo02 plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 49 items test_ctypeslib.py ....................... [ 46%] test_matlib.py ........ [ 63%] test_numpy_version.py .. [ 67%] test_public_api.py .......... [ 87%] test_reloading.py ... [ 93%] test_scripts.py X. [ 97%] test_warnings.py . [100%] =========================================================================== warnings summary =========================================================================== ..\..\..\..\Python39\lib\site-packages\numpy\tests\test_matlib.py:2 D:\Python39\lib\site-packages\numpy\tests\test_matlib.py:2: PendingDeprecationWarning: Importing from numpy.matlib is deprecated since 1.19.0. The matrix subclass is n ot the recommended way to represent matrices or deal with linear algebra (see https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). Please adjust your code to use regular ndarray. import numpy.matlib test_matlib.py::test_ones D:\Python39\lib\site-packages\numpy\tests\test_matlib.py:12: PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal wi th linear algebra (see https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). Please adjust your code to use regular ndarray. np.matrix([[ 1., 1., 1.], test_matlib.py::test_ones D:\Python39\lib\site-packages\numpy\tests\test_matlib.py:15: PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal wi th linear algebra (see https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). Please adjust your code to use regular ndarray. assert_array_equal(numpy.matlib.ones(2), np.matrix([[ 1., 1.]])) test_matlib.py::test_zeros D:\Python39\lib\site-packages\numpy\tests\test_matlib.py:19: PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal wi th linear algebra (see https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). Please adjust your code to use regular ndarray. np.matrix([[ 0., 0., 0.], test_matlib.py::test_zeros D:\Python39\lib\site-packages\numpy\tests\test_matlib.py:22: PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal wi th linear algebra (see https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). Please adjust your code to use regular ndarray. assert_array_equal(numpy.matlib.zeros(2), np.matrix([[ 0., 0.]])) test_matlib.py::test_identity D:\Python39\lib\site-packages\numpy\tests\test_matlib.py:26: PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal wi th linear algebra (see https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). Please adjust your code to use regular ndarray. assert_array_equal(x, np.matrix([[1, 0], [0, 1]])) test_matlib.py::test_eye test_matlib.py::test_eye test_matlib.py::test_rand test_matlib.py::test_randn D:\Python39\lib\site-packages\numpy\matrixlib\defmatrix.py:69: PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal with linear algebra (see https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). Please adjust your code to use regular ndarray. return matrix(data, dtype=dtype, copy=False) test_matlib.py::test_eye D:\Python39\lib\site-packages\numpy\tests\test_matlib.py:30: PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal wi th linear algebra (see https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). Please adjust your code to use regular ndarray. assert_array_equal(xc, np.matrix([[ 0, 1, 0], test_matlib.py::test_eye D:\Python39\lib\site-packages\numpy\tests\test_matlib.py:37: PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal wi th linear algebra (see https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). Please adjust your code to use regular ndarray. assert_array_equal(xf, np.matrix([[ 1, 0, 0, 0], -- Docs: https://docs.pytest.org/en/stable/warnings.html ============================================================= 48 passed, 1 xpassed, 12 warnings in 12.58s ============================================================== $ ``` ## 二、在执行脚本之前加载自定义插件 通过 -p 参数在执行脚本之前加载自定义插件,命令如下,这里因为涉及自定义插件的看法,暂时不详细演示,待后续开发自定义插件后再继续讨论使用 ```bash pytest -p mypluginmodule ``` 通过 -p 命令和在插件名称钱加no手动禁止默认插件加载,如下表示禁止doctest插件加载 ```bash pytest -p no:doctest ``` ## 三、在IDE中通过右键执行当前文件的方式执行用例 test_case01.py文件内容如下,比如在pycharm中,按照如下编写,直接右键执行即可执行测试用例 ```python import pytest def test_func1(): print("\nin test_case01.test_func1......") assert 1 == 1 def test_func2(): print("\nin test_case01.test_func2......") assert 1 == 1 if __name__ == "__main__": pytest.main() ``` 若指定pytest的参数,如下: ```python import pytest def test_func1(): print("\nin test_case01.test_func1......") assert 1 == 1 def test_func2(): print("\nin test_case01.test_func2......") assert 1 == 1 if __name__ == "__main__": pytest.main(['-s','test_case01.py']) ``` 可以在测试文件中调用自定义插件,具体使用格式如下,这里插件待后续进行插件开发时再行讨论 ```python import sys import pytest def test_func1(): print("\nin test_case01.test_func1......") assert 1 == 1 def test_func2(): print("\nin test_case01.test_func2......") assert 1 == 1 class MyPlugin: def pytest_sessionfinish(self): print("*** test run reporting finishing") if __name__ == "__main__": sys.exit(pytest.main(["-s"], plugins=[MyPlugin()])) ``` 至此,基本完成pytest的执行方式的总结了,其他更高级话题待后续继续补充
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/179
上一篇:
Redis----使用Docker安装Redis
下一篇:
Pytest----Pytest中fixture基础应用
搜索
个人成就
出版书籍
《Pytest企业级应用实战》
测试开发技术全栈公众号
测试开发技术全栈公众号
DevOps技术交流微信群
加微信邀请进群
常用网站链接
开源软件洞察
云原生技术栈全景图
Python语言官方文档
Golang官方文档
Docker官方文档
Jenkins中文用户手册
Scrapy官方文档
VUE官方文档
Harbor官方文档
openQA官方文档
云原生开源社区
开源中国
Kubernetes中文文档
Markdown语法官方教程
Kubernetes中文社区
Kubersphere官方文档
BootStrap中文网站
JavaScript中文网
NumPy官方文档
Pandas官方文档
GitLink确实开源网站
数据库排名网站
编程语言排名网站
SEO综合查询网站
数学加减法练习自动生成网站
Kickstart Generator
文章分类
最新文章
最多阅读
特别推荐
×
Close
登录
注册
找回密码
登录邮箱:
登录密码:
图片验证码:
注册邮箱:
注册密码:
邮箱验证码:
发送邮件
注册邮箱:
新的密码:
邮箱验证码:
发送邮件