使用@pytest.mark.skip()后,使用主程序文件运行pytest测试用例,该如何设置参数才能跳过@pytest.mark.skip装饰器下的测试用例?
时间: 2024-03-19 14:03:44 浏览: 20
如果你想在运行pytest时跳过使用了`@pytest.mark.skip`标记的测试用例,可以在运行pytest时添加参数`-rsx`,其中:
- `-r`选项表示输出测试结果的详细等级
- `-s`选项表示输出所有的print语句
- `-x`选项表示在第一个测试用例失败时停止测试
如果你不想输出print语句,可以省略`-s`选项,使用如下命令可以跳过`@pytest.mark.skip`标记的测试用例:
```
pytest -rsx
```
相关问题
@pytest.mark.skip 装饰器的所有使用方法详细介绍?
`@pytest.mark.skip` 装饰器用于跳过测试用例的执行。它可以应用于函数、类、模块或整个测试文件。
以下是 `@pytest.mark.skip` 装饰器的使用方法:
1. 跳过单个测试用例
```python
import pytest
def test_example():
pytest.skip("Skipping this test case")
```
2. 跳过整个测试模块
```python
import pytest
pytestmark = pytest.mark.skip(reason="Skipping this module")
@pytestmark
def test_example():
assert True
```
3. 跳过整个测试类
```python
import pytest
@pytest.mark.skip(reason="Skipping this class")
class TestExample:
def test_method1(self):
assert True
def test_method2(self):
assert True
```
4. 在运行时动态跳过测试用例
```python
import pytest
def test_example():
if not some_condition:
pytest.skip("Skipping this test case")
```
5. 跳过特定版本的 Python
```python
import pytest
@pytest.mark.skipif(sys.version_info < (3, 7), reason="Requires Python 3.7 or higher")
def test_example():
assert True
```
6. 跳过特定平台
```python
import pytest
import platform
@pytest.mark.skipif(platform.system() == "Windows", reason="Not supported on Windows")
def test_example():
assert True
```
以上是 `@pytest.mark.skip` 装饰器的几种使用方法,你可以根据需要选择适合你的方法来跳过测试用例。
@pytest.mark.parametrize 或者 @pytest.mark.allure.step用法
`@pytest.mark.parametrize` 是 pytest 提供的一个装饰器,可以用于参数化测试用例。它可以让我们在测试用例中使用多组参数来执行同一个测试用例,从而减少测试用例的编写量,提高效率。
示例代码:
```python
import pytest
@pytest.mark.parametrize("input, expected_output", [
(1, 2),
(2, 3),
(3, 4),
(4, 5),
])
def test_increment(input, expected_output):
assert input+1 == expected_output
```
`@pytest.mark.allure.step` 也是 pytest 提供的一个装饰器,用于生成测试报告。它可以将测试用例中的每个步骤作为报告的一个单独的步骤展示,从而更直观地展示测试用例的执行情况。
示例代码:
```python
import pytest
import allure
@allure.step("输入用户名和密码")
def input_username_and_password(username, password):
pass
@allure.step("点击登录按钮")
def click_login_button():
pass
@allure.step("验证登录成功")
def verify_login_success():
pass
def test_login():
input_username_and_password("testuser", "testpass")
click_login_button()
verify_login_success()
```
以上是 `@pytest.mark.parametrize` 和 `@pytest.mark.allure.step` 的基本用法。需要注意的是,`@pytest.mark.allure.step` 需要安装 `pytest-allure-adaptor` 才能正常使用。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)