How to use some_test 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.

3899.py

Source: 3899.py Github

copy
1import threading
2
3import pytest
4
5from observable import Observable, EventNotFound, HandlerNotFound
6
7
8def test_on_decorator():
9    """test event registering with the on decorator"""
10    obs = Observable()
11
12    @obs.on("on_test")
13    def on_test():
14        pass
15
16    assert on_test in obs._events["on_test"]
17
18
19def test_on():
20    """test event registering with the on method"""
21    obs = Observable()
22
23    def on_test():
24        pass
25
26    obs.on("on_test", on_test)
27    assert on_test in obs._events["on_test"]
28
29
30def test_once_decorator():
31    """test event registering with the once decorator"""
32    obs = Observable()
33
34    @obs.once("once_test")
35    def once_test():
36        pass
37
38    assert once_test in obs._events["once_test"]
39    assert obs.trigger("once_test")
40    assert once_test not in obs._events["once_test"]
41
42
43def test_once():
44    """test event registering with the once method"""
45    obs = Observable()
46
47    def once_test():
48        pass
49
50    obs.once("once_test", once_test)
51
52    assert len(obs._events["once_test"]) == 1
53    assert obs.trigger("once_test")
54    assert obs._events["once_test"] == []
55
56
57def test_on_trigger():
58    """test event triggering with event registered with on"""
59    obs = Observable()
60
61    obj = threading.local()
62    obj.called = False
63
64    @obs.on("on_test")
65    def on_test(obj):
66        obj.called = True
67
68    assert obs._events == {"on_test": [on_test]}
69    assert obs.trigger("on_test", obj)
70    assert obj.called
71
72
73def test_once_trigger():
74    """test event triggering with event registered with once"""
75    obs = Observable()
76
77    obj = threading.local()
78    obj.called = False
79
80    @obs.once("once_test")
81    def once_test(obj):
82        obj.called = True
83
84    assert len(obs._events["once_test"]) == 1
85    assert obs._events["once_test"] == [once_test]
86    assert obs.trigger("once_test", obj)
87    assert obj.called
88    assert obs._events["once_test"] == []
89    assert not obs.trigger("once_test", obj)
90
91
92def test_no_event_for_trigger():
93    """test exception raising for not existing events"""
94    obs = Observable()
95
96    assert not obs.trigger("no_existing_event")
97
98    with pytest.raises(EventNotFound):
99        obs.off("no_existing_event")
100
101
102def test_off():
103    """test obs.off method"""
104    obs = Observable()
105
106    @obs.on("on_test")
107    def on_test():
108        pass
109
110    assert obs._events["on_test"] == [on_test]
111    assert obs.trigger("on_test")
112
113    obs.off("on_test", on_test)
114
115    assert obs._events["on_test"] == []
116
117    obs.off()
118
119    assert not obs._events
120
121    @obs.on("more_than_one_event")
122    def func1():
123        pass
124
125    @obs.on("more_than_one_event")
126    def func2():
127        pass
128
129    @obs.on("more_than_one_event")
130    def func3():
131        pass
132
133    assert obs._events["more_than_one_event"] == [func1, func2, func3]
134    obs.off("more_than_one_event", func2)
135    assert obs._events["more_than_one_event"] == [func1, func3]
136    obs.off("more_than_one_event")
137    assert obs._events["more_than_one_event"] == []
138
139
140def test_off_exceptions():
141    """test exception raising in the off method"""
142    obs = Observable()
143
144    with pytest.raises(EventNotFound):
145        obs.off("non_existing_event")
146
147    @obs.on("some_event")
148    def some_assigned_handler():
149        pass
150
151    def some_non_assigned_handler():
152        pass
153
154    assert some_assigned_handler in obs._events["some_event"]
155    assert some_non_assigned_handler not in obs._events["some_event"]
156
157    with pytest.raises(HandlerNotFound):
158        obs.off("some_event", some_non_assigned_handler)
159
160
161def test_trigger_arg():
162    """test event triggering with arguments"""
163    obs = Observable()
164
165    @obs.on("some_test")
166    def some_test(some_data):
167        assert some_data == "some data"
168
169    assert obs.trigger("some_test", "some data")
170
171
172def test_trigger_args():
173    """test event triggering with argument list"""
174    obs = Observable()
175
176    @obs.on("some_test")
177    def some_test(some_data, some_other_data):
178        assert some_data is True
179        assert some_other_data is False
180
181    assert obs.trigger("some_test", *[True, False])
182
183
184def test_trigger_kwargs():
185    """test event triggering with keyword-arguments"""
186    obs = Observable()
187
188    @obs.on("some_test")
189    def some_test(some_data=True, some_other_data=False):
190        assert some_data is False
191        assert some_other_data is True
192
193    assert obs.trigger("some_test", some_other_data=True, some_data=False)
194
195
196def test_on_multiple_handlers():
197    """test event registering with the on method and multiple handlers"""
198    obs = Observable()
199
200    results = []
201
202    def some_test(*args, **kw):
203        results.append(1)
204
205    def some_test_2(*args, **kw):
206        results.append(2)
207
208    obs.on("some_test", some_test, some_test_2)
209    assert len(obs._events["some_test"]) == 2
210
211    obs.trigger("some_test")
212    assert results == [1, 2]
213
214
215def test_off_multiple_handlers():
216    """test event unregistering with the off method and multiple handlers"""
217    obs = Observable()
218
219    results = []
220
221    def some_test(*args, **kw):
222        results.append(1)
223
224    def some_test_2(*args, **kw):
225        results.append(2)
226
227    obs.on("some_test", some_test, some_test_2)
228    assert len(obs._events["some_test"]) == 2
229
230    obs.off("some_test", some_test, some_test_2)
231    assert len(obs._events["some_test"]) == 0
232    assert not obs.trigger("some_test")
233
234
235def test_multiple_inheritance():
236    """Test using class inheritance without calling Observable.__init__"""
237
238    class SomeBaseClass(object):
239        pass
240
241    class SomeBaseAndObservable(SomeBaseClass, Observable):
242        def __init__(self):
243            super(SomeBaseAndObservable, self).__init__()
244
245        def test(self):
246            self.trigger("some", True)
247
248    def some_test(data):
249        assert data is True
250
251    obj = SomeBaseAndObservable()
252    obj.on("some", some_test)
253
254    obj.test()
255
256
257def test_get_all_handlers():
258    """test get_all_handlers() after registering handlers for two events"""
259    obs = Observable()
260
261    def some_test():
262        pass
263
264    def other_test():
265        pass
266
267    assert not obs.get_all_handlers()
268
269    obs.on("some_event", some_test)
270    assert "some_event" in obs.get_all_handlers()
271    assert some_test in obs.get_all_handlers()["some_event"]
272
273    obs.on("other_event", other_test)
274    assert "other_event" in obs.get_all_handlers()
275    assert other_test in obs.get_all_handlers()["other_event"]
276    assert other_test not in obs.get_all_handlers()["some_event"]
277
278
279def test_get_handlers():
280    """test get_handlers() after registering handlers for two events"""
281    obs = Observable()
282
283    def some_test():
284        pass
285
286    def other_test():
287        pass
288
289    assert not obs.get_handlers("some_event")
290
291    obs.on("some_event", some_test)
292    assert some_test in obs.get_handlers("some_event")
293
294    obs.on("other_event", other_test)
295    assert other_test in obs.get_handlers("other_event")
296    assert other_test not in obs.get_handlers("some_event")
297
298
299def test_is_registered():
300    """test is_registered() after registering an event"""
301    obs = Observable()
302
303    def some_test():
304        pass
305
306    assert not obs.is_registered("some_event", some_test)
307    obs.on("some_event", some_test)
308    assert obs.is_registered("some_event", some_test)
309
Full Screen

test_patcher.py

Source: test_patcher.py Github

copy
1from __future__ import with_statement
2import inspect
3import unittest
4
5from nose.exc import SkipTest
6from nose.tools import eq_, raises
7
8import fudge
9
10
11class Freddie(object):
12    pass
13
14        
15def test_patch_obj():
16    class holder:
17        exc = Exception()
18    
19    patched = fudge.patch_object(holder, "exc", Freddie())
20    eq_(type(holder.exc), type(Freddie()))
21    patched.restore()
22    eq_(type(holder.exc), type(Exception()))
23
24
25def test_patch_path():
26    from os.path import join as orig_join
27    patched = fudge.patch_object("os.path", "join", Freddie())
28    import os.path
29    eq_(type(os.path.join), type(Freddie()))
30    patched.restore()
31    eq_(type(os.path.join), type(orig_join))
32
33
34def test_patch_builtin():
35    import datetime
36    orig_datetime = datetime.datetime
37    now = datetime.datetime(2010, 11, 4, 8, 19, 11, 28778)
38    fake = fudge.Fake('now').is_callable().returns(now)
39    patched = fudge.patch_object(datetime.datetime, 'now', fake)
40    try:
41        eq_(datetime.datetime.now(), now)
42    finally:
43        patched.restore()
44    eq_(datetime.datetime.now, orig_datetime.now)
45
46
47def test_patch_long_path():
48    import fudge.tests.support._for_patch
49    orig = fudge.tests.support._for_patch.some_object.inner
50    long_path = 'fudge.tests.support._for_patch.some_object.inner'
51    with fudge.patch(long_path) as fake:
52        assert isinstance(fake, fudge.Fake)
53    eq_(fudge.tests.support._for_patch.some_object.inner, orig)
54
55
56@raises(ImportError)
57def test_patch_non_existant_path():
58    with fudge.patch('__not_a_real_import_path.nested.one.two.three') as fake:
59        pass
60
61
62@raises(AttributeError)
63def test_patch_non_existant_attribute():
64    with fudge.patch('fudge.tests.support._for_patch.does.not.exist') as fake:
65        pass
66
67
68def test_patch_builtin_as_string():
69    import datetime
70    orig_datetime = datetime.datetime
71    now = datetime.datetime(2006, 11, 4, 8, 19, 11, 28778)
72    fake_dt = fudge.Fake('datetime').provides('now').returns(now)
73    patched = fudge.patch_object('datetime', 'datetime', fake_dt)
74    try:
75        # timetuple is a workaround for strange Jython behavior!
76        eq_(datetime.datetime.now().timetuple(), now.timetuple())
77    finally:
78        patched.restore()
79    eq_(datetime.datetime.now, orig_datetime.now)
80
81
82def test_decorator_on_def():
83    class holder:
84        test_called = False
85        exc = Exception()
86        
87    @fudge.with_patched_object(holder, "exc", Freddie())
88    def some_test():
89        holder.test_called = True
90        eq_(type(holder.exc), type(Freddie()))
91    
92    eq_(some_test.__name__, 'some_test')
93    some_test()
94    eq_(holder.test_called, True)
95    eq_(type(holder.exc), type(Exception()))
96
97
98def test_decorator_on_class():
99    class holder:
100        test_called = False
101        exc = Exception()
102    
103    class SomeTest(object):
104        
105        @fudge.with_patched_object(holder, "exc", Freddie())
106        def some_test(self):
107            holder.test_called = True
108            eq_(type(holder.exc), type(Freddie()))
109    
110    eq_(SomeTest.some_test.__name__, 'some_test')
111    s = SomeTest()
112    s.some_test()
113    eq_(holder.test_called, True)
114    eq_(type(holder.exc), type(Exception()))
115
116
117def test_patched_context():
118    if not hasattr(fudge, "patched_context"):
119        raise SkipTest("Cannot test with patched_context() because not in 2.5")
120    
121    class Boo:
122        fargo = "is over there"
123    
124    ctx = fudge.patched_context(Boo, 'fargo', 'is right here')
125    # simulate with fudge.patched_context():
126    ctx.__enter__()
127    eq_(Boo.fargo, "is right here")
128    ctx.__exit__(None, None, None)
129    eq_(Boo.fargo, "is over there")
130
131
132def test_base_class_attribute():
133    class Base(object):
134        foo = 'bar'
135    class Main(Base):
136        pass
137    fake = fudge.Fake()
138    p = fudge.patch_object(Main, 'foo', fake)
139    eq_(Main.foo, fake)
140    eq_(Base.foo, 'bar')
141    p.restore()
142    eq_(Main.foo, 'bar')
143    assert 'foo' not in Main.__dict__, ('Main.foo was not restored correctly')
144
145
146def test_bound_methods():
147    class Klass(object):
148        def method(self):
149            return 'foozilate'
150    instance = Klass()
151    fake = fudge.Fake()
152    p = fudge.patch_object(instance, 'method', fake)
153    eq_(instance.method, fake)
154    p.restore()
155    eq_(instance.method(), Klass().method())
156    assert inspect.ismethod(instance.method)
157    assert 'method' not in instance.__dict__, (
158                            'instance.method was not restored correctly')
159
160
161def test_staticmethod_descriptor():
162    class Klass(object):
163        @staticmethod
164        def static():
165            return 'OK'
166    fake = fudge.Fake()
167    p = fudge.patch_object(Klass, 'static', fake)
168    eq_(Klass.static, fake)
169    p.restore()
170    eq_(Klass.static(), 'OK')
171
172
173def test_property():
174    class Klass(object):
175        @property
176        def prop(self):
177            return 'OK'
178    exact_prop = Klass.prop
179    instance = Klass()
180    fake = fudge.Fake()
181    p = fudge.patch_object(instance, 'prop', fake)
182    eq_(instance.prop, fake)
183    p.restore()
184    eq_(instance.prop, 'OK')
185    eq_(Klass.prop, exact_prop)
186
187
188def test_inherited_property():
189    class SubKlass(object):
190        @property
191        def prop(self):
192            return 'OK'
193    class Klass(SubKlass):
194        pass
195    exact_prop = SubKlass.prop
196    instance = Klass()
197    fake = fudge.Fake()
198    p = fudge.patch_object(instance, 'prop', fake)
199    eq_(instance.prop, fake)
200    p.restore()
201    eq_(instance.prop, 'OK')
202    assert 'prop' not in Klass.__dict__, (
203                                'Klass.prop was not restored properly')
204    eq_(SubKlass.prop, exact_prop)
205
206
207class TestPatch(unittest.TestCase):
208
209    def setUp(self):
210        fudge.clear_expectations()
211
212    def test_decorator_on_def(self):
213
214        class holder:
215            test_called = False
216        
217        @fudge.patch('shutil.copy')
218        def some_test(copy):
219            import shutil
220            holder.test_called = True
221            assert isinstance(copy, fudge.Fake)
222            eq_(copy, shutil.copy)
223    
224        eq_(some_test.__name__, 'some_test')
225        some_test()
226        eq_(holder.test_called, True)
227        import shutil
228        assert not isinstance(shutil.copy, fudge.Fake)
229
230
231    def test_decorator_on_class(self):
232
233        class holder:
234            test_called = False
235    
236        class MyTest(object):
237
238            @fudge.patch('shutil.copy')
239            def some_test(self, copy):
240                import shutil
241                holder.test_called = True
242                assert isinstance(copy, fudge.Fake)
243                eq_(copy, shutil.copy)
244    
245        eq_(MyTest.some_test.__name__, 'some_test')
246        m = MyTest()
247        m.some_test()
248        eq_(holder.test_called, True)
249        import shutil
250        assert not isinstance(shutil.copy, fudge.Fake)
251
252    def test_patch_many(self):
253
254        class holder:
255            test_called = False
256        
257        @fudge.patch('shutil.copy',
258                     'os.remove')
259        def some_test(copy, remove):
260            import shutil
261            import os
262            holder.test_called = True
263            assert isinstance(copy, fudge.Fake)
264            assert isinstance(remove, fudge.Fake)
265            eq_(copy, shutil.copy)
266            eq_(remove, os.remove)
267    
268        eq_(some_test.__name__, 'some_test')
269        some_test()
270        eq_(holder.test_called, True)
271        import shutil
272        assert not isinstance(shutil.copy, fudge.Fake)
273        import os
274        assert not isinstance(os.remove, fudge.Fake)
275
276    def test_with_patch(self):
277
278        class holder:
279            test_called = False
280
281        def run_test():
282            with fudge.patch('shutil.copy') as copy:
283                import shutil
284                assert isinstance(copy, fudge.Fake)
285                eq_(copy, shutil.copy)
286                holder.test_called = True
287        
288        run_test()
289        eq_(holder.test_called, True)
290        import shutil
291        assert not isinstance(shutil.copy, fudge.Fake)
292
293    def test_with_multiple_patches(self):
294
295        class holder:
296            test_called = False
297
298        def run_test():
299            with fudge.patch('shutil.copy', 'os.remove') as fakes:
300                copy, remove = fakes
301                import shutil
302                import os
303                assert isinstance(copy, fudge.Fake)
304                assert isinstance(remove, fudge.Fake)
305                eq_(copy, shutil.copy)
306                eq_(remove, os.remove)
307                holder.test_called = True
308        
309        run_test()
310        eq_(holder.test_called, True)
311        import shutil
312        assert not isinstance(shutil.copy, fudge.Fake)
313        import os
314        assert not isinstance(os.remove, fudge.Fake)
315
316    def test_class_method_path(self):
317        class ctx:
318            sendmail = None
319
320        @fudge.patch('smtplib.SMTP.sendmail')
321        def test(fake_sendmail):
322            import smtplib
323            s = smtplib.SMTP()
324            ctx.sendmail = s.sendmail
325
326        test()
327        assert isinstance(ctx.sendmail, fudge.Fake)
328        import smtplib
329        s = smtplib.SMTP()
330        assert not isinstance(s.sendmail, fudge.Fake)
331
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)