How to use print_label_patch method in Nose

Best Python code snippet using nose

Run Nose automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

errorclass.py

Source: errorclass.py Github

copy
1"""
2ErrorClass Plugins
3------------------
4
5ErrorClass plugins provide an easy way to add support for custom
6handling of particular classes of exceptions.
7
8An ErrorClass plugin defines one or more ErrorClasses and how each is
9handled and reported on. Each error class is stored in a different
10attribute on the result, and reported separately. Each error class must
11indicate the exceptions that fall under that class, the label to use
12for reporting, and whether exceptions of the class should be
13considered as failures for the whole test run.
14"""
15
16from nose.plugins.base import Plugin
17from nose.pyversion import make_instancemethod
18from nose.result import TextTestResult
19from nose.util import isclass
20
21
22class MetaErrorClass(type):
23    """Metaclass for ErrorClassPlugins that allows error classes to be
24    set up in a declarative manner.
25    """
26
27    def __init__(self, name, bases, attr):
28        errorClasses = []
29        for name, detail in attr.items():
30            if isinstance(detail, ErrorClass):
31                attr.pop(name)
32                for cls in detail:
33                    errorClasses.append(
34                        (cls, (name, detail.label, detail.isfailure)))
35        super(MetaErrorClass, self).__init__(name, bases, attr)
36        self.errorClasses = tuple(errorClasses)
37
38
39class ErrorClass(object):
40    def __init__(self, *errorClasses, **kw):
41        self.errorClasses = errorClasses
42        try:
43            for key in ('label', 'isfailure'):
44                setattr(self, key, kw.pop(key))
45        except KeyError:
46            raise TypeError("%r is a required named argument for ErrorClass"
47                            % key)
48
49    def __iter__(self):
50        return iter(self.errorClasses)
51
52
53class ErrorClassPlugin(Plugin):
54    """
55    Base class for ErrorClass plugins. Subclass this class and declare the
56    exceptions that you wish to handle as attributes of the subclass.
57    """
58    __metaclass__ = MetaErrorClass
59    score = 1000
60    errorClasses = ()
61
62    def addError(self, test, err):
63        err_cls, a, b = err
64        if not isclass(err_cls):
65            return
66        classes = [e[0] for e in self.errorClasses]
67        if filter(lambda c: issubclass(err_cls, c), classes):
68            return True
69
70    def prepareTestResult(self, result):
71        if not hasattr(result, 'errorClasses'):
72            self.patchResult(result)
73        for cls, (storage_attr, label, isfail) in self.errorClasses:
74            if cls not in result.errorClasses:
75                storage = getattr(result, storage_attr, [])
76                setattr(result, storage_attr, storage)
77                result.errorClasses[cls] = (storage, label, isfail)
78
79    def patchResult(self, result):
80        result.printLabel = print_label_patch(result)
81        result._orig_addError, result.addError = \
82            result.addError, add_error_patch(result)
83        result._orig_wasSuccessful, result.wasSuccessful = \
84            result.wasSuccessful, wassuccessful_patch(result)
85        if hasattr(result, 'printErrors'):
86            result._orig_printErrors, result.printErrors = \
87                result.printErrors, print_errors_patch(result)
88        if hasattr(result, 'addSkip'):
89            result._orig_addSkip, result.addSkip = \
90                result.addSkip, add_skip_patch(result)
91        result.errorClasses = {}
92
93
94def add_error_patch(result):
95    """Create a new addError method to patch into a result instance
96    that recognizes the errorClasses attribute and deals with
97    errorclasses correctly.
98    """
99    return make_instancemethod(TextTestResult.addError, result)
100
101
102def print_errors_patch(result):
103    """Create a new printErrors method that prints errorClasses items
104    as well.
105    """
106    return make_instancemethod(TextTestResult.printErrors, result)
107
108
109def print_label_patch(result):
110    """Create a new printLabel method that prints errorClasses items
111    as well.
112    """
113    return make_instancemethod(TextTestResult.printLabel, result)
114
115
116def wassuccessful_patch(result):
117    """Create a new wasSuccessful method that checks errorClasses for
118    exceptions that were put into other slots than error or failure
119    but that still count as not success.
120    """
121    return make_instancemethod(TextTestResult.wasSuccessful, result)
122
123
124def add_skip_patch(result):
125    """Create a new addSkip method to patch into a result instance
126    that delegates to addError.
127    """
128    return make_instancemethod(TextTestResult.addSkip, result)
129
130
131if __name__ == '__main__':
132    import doctest
133
134    doctest.testmod()
135
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Python Tests on LambdaTest Cloud Grid

Execute automation tests with Nose on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)