推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
zht0858
V2EX  ›  Python

Python unittest 只在断言失败是怎么触发另一个方法函数?

  •  
  •   zht0858 · Jun 1, 2017 · 3119 views
    This topic created in 3266 days ago, the information mentioned may be changed or developed.

    在做自动化脚本开发,使用的 unittest 框架, 想在断言失败的时候截图。下面这个例子无论成功或失败都会触发指定的函数,请问怎么修改,只在失败的时候触发函数呢?

    import unittest
    class loginTests(unittest.TestCase):
        @classmethod
        def setUpClass(cls):
            pass
    
        @classmethod
        def tearDownClass(cls):
            pass
    
        def aaa(self):
            print('触发函数')
    
        def test1(self):
            i =2
            self.assertEqual(i,1,self.aaa())
    
    if __name__ == '__main__':
        unittest.main()
    
    vTexEZDota
        1
    vTexEZDota  
       Jun 1, 2017
    try:
    self.assertEqual(i,1)
    except AssertionError as e:
    self.aaa
    self.assertEqueal(i,1)

    你可以试试看。但是如果只做异常处理,这条用例会被认为正确执行了。
    jotsai
        2
    jotsai  
       Jun 1, 2017 via Android
    我是在 teardown 里判断用例是否执行失败,失败就截图
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5661 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 03:44 · PVG 11:44 · LAX 20:44 · JFK 23:44
    ♥ Do have faith in what you're doing.