小编Joh*_*opM的帖子

pytest 夹具 - 获取值并避免错误“直接调用夹具‘X’”

我已将 pytest 更新到 4.3.0,现在我需要重新编写测试代码,因为直接调用设备已被弃用。

我对 unittest.TestCase 中使用的夹具有问题,如何获取从夹具返回的值而不是对函数本身的引用?

例子 :

@pytest.fixture
def test_value():
    return 1

@pytest.mark.usefixtures("test_value")
class test_class(unittest.TestCase):
    def test_simple_in_class(self):
        print(test_value)    # prints the function reference and not the value
        print(test_value())  # fails with Fixtures are not meant to be called directly

def test_simple(test_value):
    print(test_value)  # prints 1
Run Code Online (Sandbox Code Playgroud)

如何在 test_simple_in_class() 方法中获取 test_value ?

python fixtures pytest

4
推荐指数
2
解决办法
6600
查看次数

Ansible - with_sequence 循环的条件,其变量 end 可能小于 start

我遇到了 ansible with_sequence 循环的问题,其中 end 是一个可能为零的变量。我正在寻找一种跳过循环的方法,因此我不会收到“向后计数使步幅为负”的错误。

简化示例:

- name : Register zero as var

  # The real command is something like "script | filter | wc -l"
  shell: echo 0
  register: countvar

  # I want this loop to run only when countvar is > 0

- name: Do smthg with countvar
  command: echo "{{ item }}"
  with_sequence: start=1 end={{countvar.stdout|int}}
  when: countvar.stdout|int >= 1
Run Code Online (Sandbox Code Playgroud)

loops ansible

3
推荐指数
1
解决办法
6967
查看次数

标签 统计

ansible ×1

fixtures ×1

loops ×1

pytest ×1

python ×1