recipe-578234.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:code 作者: ActiveState 项目源码 文件源码
def skip_other_tests():
    """
    Decorator which skips all tests except the one decorated with it.
    Based on original python-ideas proposal:
    http://mail.python.org/pipermail/python-ideas/2010-August/007992.html
    Working with Python from 2.5 to 3.3.

    Author: Giampaolo Rodola' <g.rodola [AT] gmail [DOT] com>
    License: MIT
    """
    import unittest
    from unittest import TextTestRunner as _TextTestRunner

    class CustomTestRunner(_TextTestRunner):
        def run(self, test):
            if test._tests:
                for t1 in test._tests:
                    for t2 in t1._tests:
                        if t2._testMethodName == self._special_name:
                            return _TextTestRunner.run(self, t2)
                raise RuntimeError("couldn't isolate test")

    def outer(fun, *args, **kwargs):
        # monkey patch unittest module
        unittest.TextTestRunner = CustomTestRunner
        if hasattr(unittest, 'runner'):  # unittest2
            unittest.runner.TextTestRunner = CustomTestRunner
        CustomTestRunner._special_name = fun.__name__

        def inner(self):
            return fun(self, *args, **kwargs)
        return inner

    return outer


# ===================================================================
# --- test
# ===================================================================
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号