pytest标记:查看测试策略、标记测试函数

就是说标记一些用例或者那些函数可以执行或者不执行,这种情况下就是我们有一些测试的这些项目还没有完成,我们可以先空下来。

一、pytest查找策略

默认情况下,pytest会递归查找当前目录下所有以test开始或结尾的Python脚本。注:(递归的意思就是这个文件夹下边还可能有子文件夹,他可以一层一层地递归查找)那么当前目录下边所有以test开头或者结尾的Python脚本,才会递归地来查找并且执行文件里边所有以test的开头或者结尾的函数或者方法

import pytest

class TestCase02(object):

    def test01(self):
        print('test01')
        # self.add()          #也可以调用add()函数

    def add(self):
        print('add')    #不需要被执行的,那么命名的时候不以test开头

    def test02(self):
        print('test02')

if __name__ == '__main__':
    pytest.main(['test02.py'])

二、标记测试函数

由于某种原因(如test_func2的功能尚未开发完成),我们只想执行指定的测试函数。在pytest中有几种方式可以解决:

第一种,显示指定函数名,通过::标记

test_no_mark.py::test_func1

demo01:

import pytest

def test01():
    print('test01')

def test02():
    print('test02')

然后再控制台上面Terminal上面输入 pytest test03.py::test01

第二种,使用模糊匹配,使用 -k 选项标识。

pytest -k func1 test_no_mark.py

demo02:

import pytest

def test_add():
    print('test01')

def test02_sub2():
    print('test02')

然后再控制台上面Terminal上面输入pytest -k add test03.py

第三种,使用pytest.mark在函数上进行标记(给用例打标签)

注册标签名,通过 .ini配置文件,格式如下:

 

 

import pytest

@pytest.mark.do
def test01():
    print('test01')

@pytest.mark.undo
def test02():
    print('test02')

然后在控制台Terminal执行pytest -m do test04.py

posted @ 2021-09-10 22:38  小黄鸭调试法  阅读(159)  评论(0)    收藏  举报
/*simplememory*/ #google_ad_c1, #google_ad_c2 {display:none;} .syntaxhighlighter a, .syntaxhighlighter div, .syntaxhighlighter code, .syntaxhighlighter table, .syntaxhighlighter table td, .syntaxhighlighter table tr, .syntaxhighlighter table tbody, .syntaxhighlighter table thead, .syntaxhighlighter table caption, .syntaxhighlighter textarea { font-size: 14px!important; } #home { opacity: 0.80; margin: 0 auto; width: 85%; min-width: 950px; background-color: #fff; padding: 30px; margin-top: 30px; margin-bottom: 50px; box-shadow: 0 2px 6px rgba(100, 100, 100, 0.3); } #blogTitle h1 { font-size: 30px; font-weight: bold; font-family: "Comic Sans MS"; line-height: 1.5em; margin-top: 20px; color: #515151; } #navList a:hover { color: #4C9ED9; text-decoration: none; } #navList a { display: block; width: 5em; height: 22px; float: left; text-align: center; padding-top: 18px; } #navigator { font-size: 15px; border-bottom: 1px solid #ededed; border-top: 1px solid #ededed; height: 50px; clear: both; margin-top: 25px; } .catListTitle { margin-top: 21px; margin-bottom: 10.5px; text-align: left; border-left: 10px solid rgba(82, 168, 236, 0.8); padding: 10px 0 14px 10px; background-color: #f5f5f5; } #ad_under_post_holder #google_ad_c1,#google_ad_c2{ display: none !important; } body { color: #000; background: url(https://i.cnblogs.com/albums/edit/2011852) fixed; background-size: 100%; background-repeat: no-repeat; font-family: "Helvetica Neue",Helvetica,Verdana,Arial,sans-serif; font-size: 12px; min-height: 101%; } #topics .postTitle { border: 0px; font-size: 200%; font-weight: bold; float: left; line-height: 1.5; width: 100%; padding-left: 5px; } div.commentform p{ margin-bottom:10px; } .comment_btn { padding: 5px 10px; height: 35px; width: 90px; border: 0 none; border-radius: 5px; background: #ddd; color: #999; cursor:pointer; font-family: "Lato", Helvetica Neue, Helvetica, Microsoft Yahei, 宋体, Arial, sans-serif; text-shadow: 0 0 1px #fff; display: inline !important; } .comment_btn:hover{ padding: 5px 10px; height: 35px; width: 90px; border: 0 none; border-radius: 5px; background: #258fb8; color: white; cursor:pointer; font-family: "Lato", Helvetica Neue, Helvetica, Microsoft Yahei, 宋体, Arial, sans-serif; text-shadow: 0 0 1px #fff; display: inline !important; } #commentform_title { background-image:none; background-repeat:no-repeat; margin-bottom:10px; padding:0; font-size:24px; } #commentbox_opt,#commentbox_opt + p { text-align:center; } .commentbox_title { width: 100%; } #tbCommentBody { font-family:'Microsoft Yahei', Microsoft Yahei, 宋体, sans-serif; margin-top:10px; max-width:100%; min-width:100%; background:white; color:#333; border:2px solid #fff; box-shadow:inset 0 0 8px #aaa; // padding:10px; height:250px; font-size:14px; min-height:120px; } .feedbackItem { font-size:14px; line-height:24px; margin:10px 0; padding:20px; background:#F2F2F2; box-shadow:0 0 5px #aaa; } .feedbackListSubtitle { font-weight:normal; } #blog-comments-placeholder, #comment_form { padding: 20px; background: #fff; -webkit-box-shadow: 1px 2px 3px #ddd; box-shadow: 1px 2px 3px #ddd; margin-bottom: 50px; } .feedback_area_title { margin-bottom: 15px; font-size: 1.8em; } .feedbackItem { border-bottom: 1px solid #CCC; margin-bottom: 10px; padding: 5px; background: rgb(248, 248, 248); } .color_shine {background: rgb(226, 242, 255);} .feedbackItem:hover {-webkit-animation-name: color_shine;-webkit-animation-duration: 2s;-webkit-animation-iteration-count: infinite;} #comment_form .title { font-weight: normal; margin-bottom: 15px; }