~crunch.io/ubuntu/precise/codespeak-lib/unstable

« back to all changes in this revision

Viewing changes to testing/test_funcargs.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2010-08-01 16:24:01 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20100801162401-g37v49d1p148alpm
Tags: 1.3.3-1
* New upstream release.
* Bump Standards-Version to 3.9.1.
* Fix typo in py.test manpage.
* Prefer Breaks: over Conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 
4
4
def test_getfuncargnames():
5
5
    def f(): pass
6
 
    assert not funcargs.getfuncargnames(f) 
 
6
    assert not funcargs.getfuncargnames(f)
7
7
    def g(arg): pass
8
8
    assert funcargs.getfuncargnames(g) == ['arg']
9
9
    def h(arg1, arg2="hello"): pass
19
19
 
20
20
def test_callspec_repr():
21
21
    cs = funcargs.CallSpec({}, 'hello', 1)
22
 
    repr(cs) 
 
22
    repr(cs)
23
23
    cs = funcargs.CallSpec({}, 'hello', funcargs._notexists)
24
24
    repr(cs)
25
25
 
30
30
                return 42
31
31
        """)
32
32
        item = testdir.getitem("def test_func(some): pass")
33
 
        exc = py.test.raises(funcargs.FuncargRequest.LookupError, 
 
33
        exc = py.test.raises(funcargs.FuncargRequest.LookupError,
34
34
            "funcargs.fillfuncargs(item)")
35
35
        s = str(exc.value)
36
36
        assert s.find("xyzsomething") != -1
48
48
        item = testdir.getitem("def test_func(some, other): pass")
49
49
        class Provider:
50
50
            def pytest_funcarg__some(self, request):
51
 
                return request.function.__name__ 
 
51
                return request.function.__name__
52
52
            def pytest_funcarg__other(self, request):
53
53
                return 42
54
54
        item.config.pluginmanager.register(Provider())
64
64
 
65
65
            class TestClass:
66
66
                def test_method(self, something):
67
 
                    pass 
 
67
                    pass
68
68
            def test_func(something):
69
 
                pass 
 
69
                pass
70
70
        """)
71
71
        item1, item2 = testdir.genitems([modcol])
72
72
        funcargs.fillfuncargs(item1)
78
78
        p = testdir.makepyfile("""
79
79
            class TestClass:
80
80
                def pytest_funcarg__something(self, request):
81
 
                    return request.instance 
 
81
                    return request.instance
82
82
                def test_method(self, something):
83
 
                    assert something is self 
 
83
                    assert something is self
84
84
        """)
85
85
        result = testdir.runpytest(p)
86
 
        assert result.stdout.fnmatch_lines([
 
86
        result.stdout.fnmatch_lines([
87
87
            "*1 passed*"
88
88
        ])
89
89
 
105
105
            def test_func(something): pass
106
106
        """)
107
107
        req = funcargs.FuncargRequest(item)
108
 
        assert req.function == item.obj 
 
108
        assert req.function == item.obj
109
109
        assert hasattr(req.module, 'test_func')
110
110
        assert req.cls is None
111
 
        assert req.function.__name__ == "test_func" 
112
 
        assert req.config == item.config 
 
111
        assert req.function.__name__ == "test_func"
 
112
        assert req.config == item.config
113
113
        assert repr(req).find(req.function.__name__) != -1
114
114
 
115
115
    def test_request_attributes_method(self, testdir):
116
116
        item, = testdir.getitems("""
117
117
            class TestB:
118
 
                def test_func(self, something): 
 
118
                def test_func(self, something):
119
119
                    pass
120
120
        """)
121
121
        req = funcargs.FuncargRequest(item)
144
144
        item = testdir.getitem("""
145
145
            def pytest_funcarg__something(request):
146
146
                return request.getfuncargvalue("something") + 1
147
 
            def test_func(something): 
 
147
            def test_func(something):
148
148
                assert something == 2
149
149
        """)
150
150
        req = funcargs.FuncargRequest(item)
151
 
        val = req.getfuncargvalue("something") 
 
151
        val = req.getfuncargvalue("something")
152
152
        assert val == 2
153
153
 
154
154
    def test_getfuncargvalue(self, testdir):
155
155
        item = testdir.getitem("""
156
156
            l = [2]
157
157
            def pytest_funcarg__something(request): return 1
158
 
            def pytest_funcarg__other(request): 
 
158
            def pytest_funcarg__other(request):
159
159
                return l.pop()
160
160
            def test_func(something): pass
161
161
        """)
162
162
        req = funcargs.FuncargRequest(item)
163
163
        py.test.raises(req.LookupError, req.getfuncargvalue, "notexists")
164
 
        val = req.getfuncargvalue("something") 
 
164
        val = req.getfuncargvalue("something")
165
165
        assert val == 1
166
 
        val = req.getfuncargvalue("something") 
 
166
        val = req.getfuncargvalue("something")
167
167
        assert val == 1
168
168
        val2 = req.getfuncargvalue("other")
169
 
        assert val2 == 2 
 
169
        assert val2 == 2
170
170
        val2 = req.getfuncargvalue("other")  # see about caching
171
171
        assert val2 == 2
172
172
        req._fillfuncargs()
175
175
    def test_request_addfinalizer(self, testdir):
176
176
        item = testdir.getitem("""
177
177
            teardownlist = []
178
 
            def pytest_funcarg__something(request): 
 
178
            def pytest_funcarg__something(request):
179
179
                request.addfinalizer(lambda: teardownlist.append(1))
180
180
            def test_func(something): pass
181
181
        """)
182
182
        req = funcargs.FuncargRequest(item)
183
 
        req.config._setupstate.prepare(item) # XXX 
 
183
        req.config._setupstate.prepare(item) # XXX
184
184
        req._fillfuncargs()
185
 
        # successively check finalization calls 
186
 
        teardownlist = item.getparent(py.test.collect.Module).obj.teardownlist 
 
185
        # successively check finalization calls
 
186
        teardownlist = item.getparent(py.test.collect.Module).obj.teardownlist
187
187
        ss = item.config._setupstate
188
 
        assert not teardownlist 
189
 
        ss.teardown_exact(item) 
 
188
        assert not teardownlist
 
189
        ss.teardown_exact(item)
190
190
        print(ss.stack)
191
191
        assert teardownlist == [1]
192
192
 
193
193
    def test_request_addfinalizer_partial_setup_failure(self, testdir):
194
194
        p = testdir.makepyfile("""
195
195
            l = []
196
 
            def pytest_funcarg__something(request): 
 
196
            def pytest_funcarg__something(request):
197
197
                request.addfinalizer(lambda: l.append(None))
198
 
            def test_func(something, missingarg): 
 
198
            def test_func(something, missingarg):
199
199
                pass
200
200
            def test_second():
201
201
                assert len(l) == 1
202
202
        """)
203
203
        result = testdir.runpytest(p)
204
 
        assert result.stdout.fnmatch_lines([
 
204
        result.stdout.fnmatch_lines([
205
205
            "*1 passed*1 error*"
206
206
            ])
207
207
 
209
209
        modcol = testdir.getmodulecol("def test_somefunc(): pass")
210
210
        item, = testdir.genitems([modcol])
211
211
        req = funcargs.FuncargRequest(item)
212
 
        assert req.fspath == modcol.fspath 
 
212
        assert req.fspath == modcol.fspath
 
213
 
 
214
def test_applymarker(testdir):
 
215
    item1,item2 = testdir.getitems("""
 
216
        class TestClass:
 
217
            def test_func1(self, something):
 
218
                pass
 
219
            def test_func2(self, something):
 
220
                pass
 
221
    """)
 
222
    req1 = funcargs.FuncargRequest(item1)
 
223
    assert 'xfail' not in item1.keywords
 
224
    req1.applymarker(py.test.mark.xfail)
 
225
    assert 'xfail' in item1.keywords
 
226
    assert 'skipif' not in item1.keywords
 
227
    req1.applymarker(py.test.mark.skipif)
 
228
    assert 'skipif' in item1.keywords
 
229
    py.test.raises(ValueError, "req1.applymarker(42)")
213
230
 
214
231
class TestRequestCachedSetup:
215
232
    def test_request_cachedsetup(self, testdir):
216
233
        item1,item2 = testdir.getitems("""
217
234
            class TestClass:
218
 
                def test_func1(self, something): 
 
235
                def test_func1(self, something):
219
236
                    pass
220
 
                def test_func2(self, something): 
 
237
                def test_func2(self, something):
221
238
                    pass
222
239
        """)
223
240
        req1 = funcargs.FuncargRequest(item1)
272
289
            def pytest_funcarg__arg2(request):
273
290
                return request.cached_setup(lambda: 17)
274
291
            def test_two_different_setups(arg1, arg2):
275
 
                assert arg1 != arg2 
 
292
                assert arg1 != arg2
276
293
        """)
277
294
        result = testdir.runpytest("-v")
278
295
        result.stdout.fnmatch_lines([
299
316
            l = []
300
317
            def pytest_funcarg__something(request):
301
318
                val = request.cached_setup(setup, teardown)
302
 
                return val 
 
319
                return val
303
320
            def setup(mycache=[1]):
304
321
                l.append(mycache.pop())
305
 
                return l 
 
322
                return l
306
323
            def teardown(something):
307
324
                l.remove(something[0])
308
325
                l.append(2)
312
329
                assert something == [1]
313
330
        """)
314
331
        testdir.makepyfile(test_1="""
315
 
            import test_0 # should have run already 
 
332
            import test_0 # should have run already
316
333
            def test_check_test0_has_teardown_correct():
317
334
                assert test_0.l == [2]
318
335
        """)
332
349
        metafunc = funcargs.Metafunc(func)
333
350
        assert len(metafunc.funcargnames) == 1
334
351
        assert 'arg1' in metafunc.funcargnames
335
 
        assert metafunc.function is func 
 
352
        assert metafunc.function is func
336
353
        assert metafunc.cls is None
337
354
 
338
355
    def test_addcall_no_args(self):
360
377
    def test_addcall_param(self):
361
378
        def func(arg1): pass
362
379
        metafunc = funcargs.Metafunc(func)
363
 
        class obj: pass 
364
 
        metafunc.addcall(param=obj) 
365
 
        metafunc.addcall(param=obj) 
366
 
        metafunc.addcall(param=1) 
 
380
        class obj: pass
 
381
        metafunc.addcall(param=obj)
 
382
        metafunc.addcall(param=obj)
 
383
        metafunc.addcall(param=1)
367
384
        assert len(metafunc._calls) == 3
368
 
        assert metafunc._calls[0].param == obj 
369
 
        assert metafunc._calls[1].param == obj 
 
385
        assert metafunc._calls[0].param == obj
 
386
        assert metafunc._calls[1].param == obj
370
387
        assert metafunc._calls[2].param == 1
371
388
 
372
389
    def test_addcall_funcargs(self):
373
390
        def func(arg1): pass
374
391
        metafunc = funcargs.Metafunc(func)
375
 
        class obj: pass 
 
392
        class obj: pass
376
393
        metafunc.addcall(funcargs={"x": 2})
377
394
        metafunc.addcall(funcargs={"x": 3})
378
395
        assert len(metafunc._calls) == 2
386
403
            # assumes that generate/provide runs in the same process
387
404
            import py
388
405
            def pytest_generate_tests(metafunc):
389
 
                metafunc.addcall(param=metafunc) 
 
406
                metafunc.addcall(param=metafunc)
390
407
 
391
408
            def pytest_funcarg__metafunc(request):
392
409
                assert request._pyfuncitem._genid == "0"
393
 
                return request.param 
 
410
                return request.param
394
411
 
395
412
            def test_function(metafunc, pytestconfig):
396
413
                assert metafunc.config == pytestconfig
418
435
    def test_addcall_with_two_funcargs_generators(self, testdir):
419
436
        testdir.makeconftest("""
420
437
            def pytest_generate_tests(metafunc):
421
 
                assert "arg1" in metafunc.funcargnames 
 
438
                assert "arg1" in metafunc.funcargnames
422
439
                metafunc.addcall(funcargs=dict(arg1=1, arg2=2))
423
440
        """)
424
441
        p = testdir.makepyfile("""
427
444
 
428
445
            class TestClass:
429
446
                def test_myfunc(self, arg1, arg2):
430
 
                    assert arg1 == arg2 
 
447
                    assert arg1 == arg2
431
448
        """)
432
449
        result = testdir.runpytest("-v", p)
433
 
        assert result.stdout.fnmatch_lines([
434
 
            "*test_myfunc*0*PASS*", 
435
 
            "*test_myfunc*1*FAIL*", 
 
450
        result.stdout.fnmatch_lines([
 
451
            "*test_myfunc*0*PASS*",
 
452
            "*test_myfunc*1*FAIL*",
436
453
            "*1 failed, 1 passed*"
437
454
        ])
438
455
 
440
457
        p = testdir.makepyfile("""
441
458
            def pytest_generate_tests(metafunc):
442
459
                metafunc.addcall(param=10)
443
 
                metafunc.addcall(param=20) 
 
460
                metafunc.addcall(param=20)
444
461
 
445
462
            def pytest_funcarg__arg1(request):
446
463
                return request.param
451
468
                assert arg1 in (10, 20)
452
469
        """)
453
470
        result = testdir.runpytest("-v", p)
454
 
        assert result.stdout.fnmatch_lines([
455
 
            "*test_func1*0*PASS*", 
456
 
            "*test_func1*1*FAIL*", 
 
471
        result.stdout.fnmatch_lines([
 
472
            "*test_func1*0*PASS*",
 
473
            "*test_func1*1*FAIL*",
457
474
            "*test_func2*PASS*",
458
475
            "*1 failed, 3 passed*"
459
476
        ])
461
478
    def test_generate_plugin_and_module(self, testdir):
462
479
        testdir.makeconftest("""
463
480
            def pytest_generate_tests(metafunc):
464
 
                assert "arg1" in metafunc.funcargnames 
 
481
                assert "arg1" in metafunc.funcargnames
465
482
                metafunc.addcall(id="world", param=(2,100))
466
483
        """)
467
484
        p = testdir.makepyfile("""
475
492
 
476
493
            class TestClass:
477
494
                def test_myfunc(self, arg1, arg2):
478
 
                    assert arg1 == arg2 
 
495
                    assert arg1 == arg2
479
496
        """)
480
497
        result = testdir.runpytest("-v", p)
481
 
        assert result.stdout.fnmatch_lines([
482
 
            "*test_myfunc*hello*PASS*", 
483
 
            "*test_myfunc*world*FAIL*", 
 
498
        result.stdout.fnmatch_lines([
 
499
            "*test_myfunc*hello*PASS*",
 
500
            "*test_myfunc*world*FAIL*",
484
501
            "*1 failed, 1 passed*"
485
502
        ])
486
503
 
494
511
                    assert hello == "world"
495
512
        """)
496
513
        result = testdir.runpytest("-v", p)
497
 
        assert result.stdout.fnmatch_lines([
498
 
            "*test_myfunc*hello*PASS*", 
 
514
        result.stdout.fnmatch_lines([
 
515
            "*test_myfunc*hello*PASS*",
499
516
            "*1 passed*"
500
517
        ])
501
518
 
511
528
                    self.x = 1
512
529
        """)
513
530
        result = testdir.runpytest("-v", p)
514
 
        assert result.stdout.fnmatch_lines([
515
 
            "*test_func*0*PASS*", 
516
 
            "*test_func*1*PASS*", 
 
531
        result.stdout.fnmatch_lines([
 
532
            "*test_func*0*PASS*",
 
533
            "*test_func*1*PASS*",
517
534
            "*2 pass*",
518
535
        ])
519
536
 
559
576
    clscol.obj = lambda arg1: None
560
577
    clscol.funcargs = {}
561
578
    funcargs.fillfuncargs(clscol)
562
 
    assert clscol.funcargs['arg1'] == 42 
 
579
    assert clscol.funcargs['arg1'] == 42
563
580
 
564
581
 
565
582
def test_funcarg_lookup_error(testdir):
575
592
        "*available funcargs*",
576
593
        "*1 error*",
577
594
    ])
578
 
    assert "INTERNAL" not in result.stdout.str() 
 
595
    assert "INTERNAL" not in result.stdout.str()