pytest框架简介:用例编写规则、执行用例

#规则:
#①文件命名以test开头
#②类的名称
#③不能有初始化方法
#④测试函数必须以test开头
#⑤断言assert即可

import pytest

class TestLoginCase(object):  #测试类以Test开头

    # def __init__(self): 不能定义init方法

    def test01(self):    #测试函数以test开头
        print('test01')
        assert 1 ==1
    def test02(self):
        print('test02')
        assert 1 == 2
if __name__ == '__main__':
    pytest.main(['test_pytest.py'])   #main方法里面有个列表,列表里面的参数指的是测试模块的名称
                                        # 输出结果collected(代表一个集合)这个集合里面有2 个item,也说执行两个测试

# if __name__ == '__main__':
#     pytest.main(['-s','test_pytest.py'])   #加个参数-s,才能在控制台打印print内容,否则不会输出

# if __name__ == '__main__':
#     pytest.main(['-v','test_pytest.py'])    #-v,用于显示每个测试函数的执行结果,一般都加这个

# if __name__ == '__main__':
#     pytest.main(['-q','test_pytest.py'])     #-q,用于显示整体测试结果

# if __name__ == '__main__':
#     pytest.main(['-x','test_pytest.py'])   #-x,在第一个错误或测试失败时立即退出,-x或者--exitfirst

# if __name__ == '__main__':
#     pytest.main(['-h','test_pytest.py'])    #-h,帮助

# if __name__ == '__main__':
#     pytest.main(['-vs','test_pytest.py'])    #一般用-v合-s组合这两个参数

执行测试:

①配置pycharm

Tools - 》Python integrated tools - 》Default test runner

②main方法

pytest.main(['sv','模块名.py’])

pytest.main()   注:main() 方法默认执行当前文件中所有以“test”开头的函数

③命令行

Python控制台下面在Terminal 执行pytest -s -v test.py

命令行的执行好处就是将来我们想把这个测试集成到其他环境里面去,比如集成到Jenkis里面。

posted @ 2021-09-10 21:46  小黄鸭调试法  阅读(164)  评论(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; }