pytest中variables用法
时间: 2023-07-08 15:33:03 浏览: 280
在pytest中,variables是一个插件,它可以让你在测试用例中使用自定义变量。使用variables插件,你可以在pytest.ini文件中定义变量,然后在测试用例中使用这些变量。
以下是variables插件的用法示例:
1. 在pytest.ini文件中定义变量:
```
[pytest]
variables =
foo: 123
bar: hello world
```
在这个例子中,我们定义了两个变量foo和bar。foo的值是123,bar的值为"hello world"。
2. 在测试用例中使用变量:
```
def test_variables(variables):
assert variables['foo'] == 123
assert variables['bar'] == 'hello world'
```
在这个例子中,我们使用了variables fixture来获取变量。我们断言foo的值为123,bar的值为"hello world"。
需要注意的是,如果变量值中包含空格或特殊字符,需要使用引号将其括起来。例如:
```
[pytest]
variables =
foo: 123
bar: "hello world"
baz: 'foo"bar'
```
在这个例子中,我们定义了三个变量:foo,bar和baz。bar的值包含空格,因此我们将其用双引号括起来。baz的值包含双引号,因此我们将其用单引号括起来。
除了在pytest.ini文件中定义变量,你还可以在命令行中使用--variables选项来定义变量,例如:
```
pytest --variables foo=123 bar="hello world"
```
这将定义两个变量foo和bar,并将它们的值分别设置为123和"hello world"。你可以在测试用例中使用这些变量,例如:
```
def test_variables(variables):
assert variables['foo'] == 123
assert variables['bar'] == 'hello world'
```
阅读全文
相关推荐
















