pytest

解决pytest运行时报错ModuleNotFoundError

牧云@^-^@ 提交于 2019-11-29 10:35:34
封装代码后通过pytest运行时报错模块没找到错误。 导入模块前面加上这2句话解决 No modlue name '模块名'的报错。 import os,sys sys.path.append(os.getcwd()) #告诉pytest运行前先检索当前路径 报错原因pytest运行时没有检索当前目录自己导入的模块。 报错截图: 来源: https://www.cnblogs.com/xiamaojjie/p/11515708.html

Is there a way to sandbox test execution with pytest, especially filesystem access?

寵の児 提交于 2019-11-29 10:22:19
问题 I'm interested in executing potentially untrusted tests with pytest in some kind of sandbox, like docker, similarly to what continuous integration services do. I understand that to properly sandbox a python process you need OS-level isolation, like running the tests in a disposable chroot/container, but in my use case I don't need to protect against intentionally malicious code, only from dangerous behaviour of pairing "randomly" functions with arguments. So lesser strict sandboxing may still

python:pytest中的setup和teardown

情到浓时终转凉″ 提交于 2019-11-29 10:14:37
原文: https://www.cnblogs.com/peiminer/p/9376352.html   之前我写的unittest的setup和teardown,还有setupClass和teardownClass(需要配合@classmethod装饰器一起使用),接下来就介绍pytest的类似于这类的固件。 (1.setup_function、teardown_function 2.setup_class、teardown_class 3.setup_method、teardown_method 4.setup_module、teardown_module) setup/teardown和unittest里面的setup/teardown是一样的功能,这里setup_method和teardown_method的功能和setup/teardown功能是一样的,优先级是先执行setup_method,在执行setup。一般二者用其中一个即可 , 就不详细介绍了。setup_class和teardown_class等价于unittest里面的setupClass和teardownClass 一、函数级的(setup_function、teardown_function)只对函数用例生效,而且不在类中使用 #!/usr/bin/env/python # -*-coding:utf

Dependencies between files with pytest-dependency?

有些话、适合烂在心里 提交于 2019-11-29 10:10:32
I'm working on a functional test suite using pytest with pytest-dependency. I 99% love these tools, but I can't figure out how to have a test in one file depend on a test in another file. Ideally, I'd like to have zero changes required to the dependee, and only change things in the depender. I'd like tests to be able to depend on test_one both like this: # contents of test_one.py @pytest.mark.dependency() def test_one(): # do stuff @pytest.mark.dependency(depends=["test_one"]) def test_point_one(): # do stuff And like this: # contents of test_two.py @pytest.mark.dependency(depends=["test_one"]

pytest的一些实用插件实践

戏子无情 提交于 2019-11-29 09:51:11
1.多重校验 pytest-assume 简单的校验assert,虽然可以写多个assert def test_add1(self): assert add(2,3)==5 assert add(1,3)==3 assert add(2,5)==7 由于第二个断言失败,那么下面的断言就不会执行。 所以如果需要多个断言,都执行就需要第三方插件 pytest-assume 安装命令: pip install pytest-assume 示例: def test_add2(self): pytest.assume(add(1,2)==3) pytest.assume(add(1,4)==3) pytest.assume(add(2,2)==4) 这边即使第二个断言失败了,第三个断言还是会继续执行。 2.设定执行顺序 pytest-ordering 对于一些上下文依赖的,有时候可能需要设定一些特定执行顺序,pytest的ordering插件,就很好的解决了这个问题 安装命令 pip install pytest-ordering 示例脚本如下: def test_order1(): print ("first test") assert True def test_order2(): print ("second test") assert True 没有加上ordering,执行顺序是

py.test: how to get the current test's name from the setup method?

女生的网名这么多〃 提交于 2019-11-29 09:06:34
I am using py.test and wonder if/how it is possible to retrieve the name of the currently executed test within the setup method that is invoked before running each test. Consider this code: class TestSomething(object): def setup(self): test_name = ... def teardown(self): pass def test_the_power(self): assert "foo" != "bar" def test_something_else(self): assert True Right before TestSomething.test_the_power becomes executed, I would like to have access to this name in setup as outlined in the code via test_name = ... so that test_name == "TestSomething.test_the_power" . Actually, in setup , I

py.test skips test class if constructor is defined

爷,独闯天下 提交于 2019-11-29 09:02:35
I have following unittest code running via py.test. Mere presence of the constructor make the entire class skip when running py.test -v -s collected 0 items / 1 skipped Can anyone please explain to me this behaviour of py.test? I am interested in understanding py.test behaviour, I know the constructor is not needed. Thanks, Zdenek class TestClassName(object): def __init__(self): pass def setup_method(self, method): print "setup_method called" def teardown_method(self, method): print "teardown_method called" def test_a(self): print "test_a called" assert 1 == 1 def test_b(self): print "test_b

pytest之参数化

一曲冷凌霜 提交于 2019-11-29 08:37:35
先简单说说什么是参数化,已百度为例平时我们测试搜索,每次我们测试一个不同的搜索内容,都需要更改参数的值。在这个过程里面,除了数据在变动以外,其他步骤都是重复的。 这个时候我们就可以使用参数化的方式来代替数据的变动。参数化顾名思义就是把不同的参数,写到一个列表里,或者说写到一个集合里面。然后让程序自动去这个列表里面取值,直到列表为空便结束。 import pytest from appium import webdriver import time # todo pytest里面类名也要已test开头 class Test_Search_(): def setup_class(self): desired_caps = { "platformName": "Android", "platformVersion": "5.1", "deviceName": "127.0.0.1:62001", "appPackage": "com.android.settings", "appActivity": ".Settings", "noreset": "True" } # todo 获得驱动对象 self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) @pytest.mark

docker学习7-Dockerfile制作自己的镜像文件

吃可爱长大的小学妹 提交于 2019-11-29 06:38:13
Dockerfile简介 Dockerfile是用来构建Docker镜像的构建文件,是由一系列命令和参数构成的脚本。 简单来说,Dockerfile就是把我们安装环境的每个步骤和指令,放到一个文件,最后一键执行,最后做成一个你想要的环境。 Dockerfile是用来构建Docker镜像的构建文件,是由一系列命令和参数构成的脚本。 Docker构建三步曲: 编写dockerfile文件 docker build 构建image镜像文件 docker run 运行容器 Dockerfile相关指令 Dockerfile 是一个包含创建镜像所有命令的文本文件,通过docker build命令可以根据 Dockerfile 的内容构建镜像, 在介绍如何构建之前先介绍下 Dockerfile 的基本语法结构。 Dockerfile 有以下指令选项: FROM 基础镜像,当前新镜像是基于哪个镜像的 MAINTAINER 镜像维护者的姓名和邮箱地址 RUN 容器构建时需要运行的命令 CMD 指定一个容器启动时要运行的命令。dockerfile中可以有多个CMD指令,但只有最后一个生效,CMD会被docker run之后的参数替换。 EXPOSE 当前容器对外暴露的端口号 ENV 用来在构建镜像过程中设置环境变量 ADD

Py.test command not found, but library is installed

夙愿已清 提交于 2019-11-29 06:18:31
There are already two posts on stack overflow on this topic; however, none of them have resolved or addressed my specific situation. I have installed pytest via pip install pytest . I am able to import the library in Python as well. The problem is that when I try to use the py.test command in Terminal, I get py.test: command not found . Does anyone have any insight as to why I am not able to use the command in the terminal? EDIT: It even shows up as an installed package: $ pip list cycler (0.9.0) matplotlib (1.5.1) numpy (1.10.1) pip (8.1.0) py (1.4.31) pyparsing (2.0.7) pytest (2.9.0) python