我正在使用pytest和pytest-dependency的功能测试套件.我99%喜欢这些工具,但我无法弄清楚如何在一个文件中进行测试取决于另一个文件中的测试.理想情况下,我希望对dependee进行零更改,并且只更改依赖项中的内容.我希望测试能够依赖于test_one这两个:
# 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
Run Code Online (Sandbox Code Playgroud)
像这样:
# contents of test_two.py
@pytest.mark.dependency(depends=["test_one"])
def test_two():
# do stuff
Run Code Online (Sandbox Code Playgroud)
当我pytest test_one.py正确地运行它命令事物(并且test_point_one如果test_one失败pytest test_two.py则跳过),但是当我运行时,它会跳过test_two.
我已经尝试添加import test_one到test_two.py无济于事,并且验证导入实际上是正确导入的 - 它不仅仅是被pytest传递"哦,嘿,我已经完成了测试,我没有任何东西可以'跳过!懒惰的万岁!"
我知道我可以在技术上把test_two()在test_one.py和它的工作,但我不想只转储每个测试在一个文件中(这是什么,这将最终退化为).我试图通过把所有东西放在正确的架子上来保持整洁,而不是把它全部塞进壁橱里.
此外,我意识到如果这是我可以做的事情,那么存在创建循环依赖的可能性.我没关系.如果我这样开枪自己,那就说实话,我应该得到它.
我正在 pycharm 中使用 pytest 编写测试。测试分为不同的类别。
我想指定某些类必须在其他类之前运行。
我在 stackoverflow 上看到了各种问题(例如指定从文件运行 pytest 测试以及如何在所有其他测试之前运行方法)。
这些和其他各种问题想要选择特定的函数按顺序运行。fixtures据我了解,这可以使用或 with来完成pytest ordering。
我不关心每个类的哪些函数首先运行。我所关心的是课程按照我指定的顺序运行。这可能吗?
我面临着 pytest 装置的一个小问题,非常感谢您的帮助。
我有一些功能装置,如下所述。为了简单起见,我没有展示其实现。
@pytest.fixture()
def get_driver():
pass
@pytest.fixture()
def login(get_driver):
pass
@pytest.fixture()
def settings(login):
pass
Run Code Online (Sandbox Code Playgroud)
问题是我还需要一个(会话级别)固定装置,它可以在运行第一个测试用例之前进行设置。(实际开始测试)。即进入设置页面并创建一些设置。(登录后)
现在的问题是我不能使用会话级别的固定装置这样做,因为我不能在会话级别使用功能级别的固定装置。或者我可以吗?
@pytest.fixture(scope="session")
def setup(settings):
settings.create_settings()
pass
Run Code Online (Sandbox Code Playgroud) 我有一些带有测试的脚本,我需要按照我明确定义的执行顺序运行这些测试。
看起来像:
# one.py
import some lib
class Foo():
def makesmth(self)
script
Run Code Online (Sandbox Code Playgroud)
然后我制作了测试文件:
# test_one.py
import pytest
import some lib
class TestFoo():
def test_makesmth(self):
try/except/else assert etc.
Run Code Online (Sandbox Code Playgroud)
所以它看起来简单而正确。当我运行文件 test_one.py 时一切正常。我的脚本测试包看起来像:
package/
|-- __init__.py
|-- scripts
| |-- one.py
| |-- two.py
|-- tests
| |-- test_one.py
| |-- test_two.py
Run Code Online (Sandbox Code Playgroud)
当我尝试收集测试时
pytest --collect-only
Run Code Online (Sandbox Code Playgroud)
它给出了非字母顺序和随机的测试顺序。
我可以在哪里写有关测试顺序的信息?非字母,就像我想像 b、a、c、e、d 一样开始测试 - 不是随机的,不是按字母顺序的
试图制作文件tests.py:
import pytest
from tests.test_one import TestFoo
from tests.test_two import TestBoo etc.
Run Code Online (Sandbox Code Playgroud)
当我试图运行它,显示错误,因为这些进口是在我不明白的方式进行(试图使一个命名为testFoo b TestBoo并重新命名该方法定义的方式测试文件,但仍然是没有按”工作)。
对不起,如果我的问题看起来不专业,我是初级问答,几乎没有自动测试经验。
假设我有以下 pytest 脚本:
import pytest
def test_one():
pass
def test_two():
pass
@pytest.mark.slow
def test_three():
pass
Run Code Online (Sandbox Code Playgroud)
是否有一个命令可以用来运行slow最后带有标记的所有测试?我知道我可以使用两个 pytest 命令来完成此操作,但使用单个命令来完成此操作会很棒:
pytest -v -m "not slow"
# test_markers.py::test_one PASSED
# test_markers.py::test_two PASSED
pytest -v -m slow
# test_markers.py::test_three PASSED
Run Code Online (Sandbox Code Playgroud) @pytest.mark.incremental
class Test_aws():
def test_case1(self):
----- some code here ----
result = someMethodTogetResult
assert result[0] == True
orderID = result[1]
def test_case2(self):
result = someMethodTogetResult # can be only perform once test case 1 run successfully.
assert result == True
def test_deleteOrder_R53HostZonePrivate(self):
result = someMethodTogetResult
assert result[0] == True
Run Code Online (Sandbox Code Playgroud)
当前行为是:如果测试1通过,则测试2运行,如果测试2通过,则测试3运行。
我需要的是:如果test_case 1通过则应该运行test_case 3。test_case 2不应更改任何行为。有什么想法吗?
pytest ×6
python ×5
python-2.7 ×2
class ×1
fixtures ×1
function ×1
pycharm ×1
python-3.x ×1
qa ×1
selenium ×1