4
4
def test_getfuncargnames():
6
assert not funcargs.getfuncargnames(f)
6
assert not funcargs.getfuncargnames(f)
8
8
assert funcargs.getfuncargnames(g) == ['arg']
9
9
def h(arg1, arg2="hello"): pass
20
20
def test_callspec_repr():
21
21
cs = funcargs.CallSpec({}, 'hello', 1)
23
23
cs = funcargs.CallSpec({}, 'hello', funcargs._notexists)
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)")
36
36
assert s.find("xyzsomething") != -1
48
48
item = testdir.getitem("def test_func(some, other): pass")
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):
54
54
item.config.pluginmanager.register(Provider())
66
66
def test_method(self, something):
68
68
def test_func(something):
71
71
item1, item2 = testdir.genitems([modcol])
72
72
funcargs.fillfuncargs(item1)
78
78
p = testdir.makepyfile("""
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
85
85
result = testdir.runpytest(p)
86
assert result.stdout.fnmatch_lines([
86
result.stdout.fnmatch_lines([
105
105
def test_func(something): pass
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
115
115
def test_request_attributes_method(self, testdir):
116
116
item, = testdir.getitems("""
118
def test_func(self, something):
118
def test_func(self, something):
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
150
150
req = funcargs.FuncargRequest(item)
151
val = req.getfuncargvalue("something")
151
val = req.getfuncargvalue("something")
154
154
def test_getfuncargvalue(self, testdir):
155
155
item = testdir.getitem("""
157
157
def pytest_funcarg__something(request): return 1
158
def pytest_funcarg__other(request):
158
def pytest_funcarg__other(request):
160
160
def test_func(something): pass
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")
166
val = req.getfuncargvalue("something")
166
val = req.getfuncargvalue("something")
168
168
val2 = req.getfuncargvalue("other")
170
170
val2 = req.getfuncargvalue("other") # see about caching
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
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)
191
191
assert teardownlist == [1]
193
193
def test_request_addfinalizer_partial_setup_failure(self, testdir):
194
194
p = testdir.makepyfile("""
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):
200
200
def test_second():
201
201
assert len(l) == 1
203
203
result = testdir.runpytest(p)
204
assert result.stdout.fnmatch_lines([
204
result.stdout.fnmatch_lines([
205
205
"*1 passed*1 error*"
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
214
def test_applymarker(testdir):
215
item1,item2 = testdir.getitems("""
217
def test_func1(self, something):
219
def test_func2(self, something):
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)")
214
231
class TestRequestCachedSetup:
215
232
def test_request_cachedsetup(self, testdir):
216
233
item1,item2 = testdir.getitems("""
218
def test_func1(self, something):
235
def test_func1(self, something):
220
def test_func2(self, something):
237
def test_func2(self, something):
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):
277
294
result = testdir.runpytest("-v")
278
295
result.stdout.fnmatch_lines([
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
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)
364
metafunc.addcall(param=obj)
365
metafunc.addcall(param=obj)
366
metafunc.addcall(param=1)
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
372
389
def test_addcall_funcargs(self):
373
390
def func(arg1): pass
374
391
metafunc = funcargs.Metafunc(func)
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
388
405
def pytest_generate_tests(metafunc):
389
metafunc.addcall(param=metafunc)
406
metafunc.addcall(param=metafunc)
391
408
def pytest_funcarg__metafunc(request):
392
409
assert request._pyfuncitem._genid == "0"
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))
424
441
p = testdir.makepyfile("""
429
446
def test_myfunc(self, arg1, arg2):
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*"
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)
445
462
def pytest_funcarg__arg1(request):
446
463
return request.param
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))
467
484
p = testdir.makepyfile("""
477
494
def test_myfunc(self, arg1, arg2):
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*"
494
511
assert hello == "world"
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*",
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
565
582
def test_funcarg_lookup_error(testdir):