Novice学Pytest(8)-使用自定义标记mark

一、前言

  pytest可以支持自定义标记,自定义标记可以把一个移动端项目划分为多个模块,然后指定模块名执行。工作中我们会写yy Android和yy iOS的用例脚本,会指定哪些用例是在yy Android下执行的,哪些用例是yy iOS下执行的,在运行脚本时指定mark即可

二、示例代码

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 
 4 """
 5 __Title__   =
 6 __Time__    =  2021/8/21 12:14
 7 __Author__  =  Isaymore
 8 __Blog__    =  https://www.cnblogs.com/huainanhai/
 9 """
10 
11 import pytest
12 
13 @pytest.mark.yyAndroid
14 def test_yy_Android():
15     print("测试yy Android")
16 
17 @pytest.mark.yyiOS
18 def test_yy_iOS():
19     print("测试yy iOS")
20 
21 @pytest.mark.yyiOS
22 def test_yy_iOS1():
23     print("继续测试yy iOS")
24 
25 @pytest.mark.tieba
26 class Testclass:
27     def test_method(self):
28         print("测试贴吧")
29 
30 def test_noMark():
31     print("没有标记")

  cmd or pycharm terminal输入命令:pytest -s -m yyAndroid custom_mark.py

  执行结果:

  Tips:如何避免warnings

  • 创建一个pytest.ini文件
  • 加上自定义mark

三、标记取反

  如果不想标记yyAndroid的用例,直接取反即可

  输入命令:pytest -s -m "not yyAndroid" custom_mark.py

  执行结果:

四、执行多个自定义标记的用例

  输入命令:pytest -s -m "yyAndroid or yyiOS" custom.mark.py

  执行结果:

 

参考链接:https://www.cnblogs.com/poloyy/p/12669068.html

posted @ 2022-04-22 22:45  方缘  阅读(51)  评论(0编辑  收藏  举报