本文整理汇总了Python中py.test方法的典型用法代码示例。如果您正苦于以下问题:Python py.test方法的具体用法?Python py.test怎么用?Python py.test使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类py
的用法示例。
在下文中一共展示了py.test方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_worker
# 需要导入模块: import py [as 别名]
# 或者: from py import test [as 别名]
def get_worker(loc):
try:
# use py.test module interface if it's installed
import py.test
# check that we have at least version 2.1.2
try:
py.test.__version__
except AttributeError:
raise ImportError
if _version_too_old(py.test.__version__, (2,1,2)):
raise ImportError
else:
return py.test.cmdline.main
except ImportError:
# try to locate the script
script = os.path.join(loc, SCRIPT)
if os.path.exists(script):
return lambda args: subtest(script, args)
else:
raise Exception('Could not find self-contained py.test script in'
'"%s"'%script)
示例2: testConvolution2DNode_arguments
# 需要导入模块: import py [as 别名]
# 或者: from py import test [as 别名]
def testConvolution2DNode_arguments():
# filters must be 3D
filters = numx.random.random((5,4))
py.test.raises(mdp.NodeException,
"mdp.nodes.Convolution2DNode(filters)")
filters = numx.random.random((5,4,2,2))
py.test.raises(mdp.NodeException,
"mdp.nodes.Convolution2DNode(filters)")
# filters must be array
filters = [[[2.]]]
py.test.raises(mdp.NodeException,
"mdp.nodes.Convolution2DNode(filters)")
filters = numx.random.random((1,1,1))
with py.test.raises(mdp.NodeException):
mdp.nodes.Convolution2DNode(filters, approach='bug')
with py.test.raises(mdp.NodeException):
mdp.nodes.Convolution2DNode(filters, mode='bug')
with py.test.raises(mdp.NodeException):
mdp.nodes.Convolution2DNode(filters, boundary='bug')
示例3: test_inverse
# 需要导入模块: import py [as 别名]
# 或者: from py import test [as 别名]
def test_inverse(klass, init_args, inp_arg_gen,
sup_arg_gen, execute_arg_gen):
args = call_init_args(init_args)
inp = inp_arg_gen()
# take the first available dtype for the test
dtype = klass(*args).get_supported_dtypes()[0]
args = call_init_args(init_args)
node = klass(dtype=dtype, *args)
_train_if_necessary(inp, node, sup_arg_gen)
extra = [execute_arg_gen(inp)] if execute_arg_gen else []
out = node.execute(inp, *extra)
# compute the inverse
rec = node.inverse(out)
# cast inp for comparison!
inp = inp.astype(dtype)
assert_array_almost_equal_diff(rec, inp, decimal-3)
assert rec.dtype == dtype
示例4: test_Node_save
# 需要导入模块: import py [as 别名]
# 或者: from py import test [as 别名]
def test_Node_save():
test_list = [1,2,3]
generic_node = mdp.Node()
generic_node.dummy_attr = test_list
# test string save
copy_node_pic = generic_node.save(None)
copy_node = cPickle.loads(copy_node_pic)
assert generic_node.dummy_attr == copy_node.dummy_attr,\
'Node save (string) method did not work'
copy_node.dummy_attr[0] = 10
assert generic_node.dummy_attr != copy_node.dummy_attr,\
'Node save (string) method did not work'
# test file save
dummy_file = tempfile.mktemp(prefix='MDP_', suffix=".pic",
dir=py.test.mdp_tempdirname)
generic_node.save(dummy_file, protocol=1)
dummy_file = open(dummy_file, 'rb')
copy_node = cPickle.load(dummy_file)
assert generic_node.dummy_attr == copy_node.dummy_attr,\
'Node save (file) method did not work'
copy_node.dummy_attr[0] = 10
assert generic_node.dummy_attr != copy_node.dummy_attr,\
'Node save (file) method did not work'
示例5: testContextDecorator
# 需要导入模块: import py [as 别名]
# 或者: from py import test [as 别名]
def testContextDecorator():
"""Test the with_extension function decorator."""
class Test1ExtensionNode(mdp.ExtensionNode):
extension_name = "__test1"
def _testtest(self):
pass
@mdp.with_extension("__test1")
def test():
return mdp.get_active_extensions()
# check that the extension is activated
assert mdp.get_active_extensions() == []
active = test()
assert active == ["__test1"]
assert mdp.get_active_extensions() == []
# check that it is only deactiveted if it was activated there
mdp.activate_extension("__test1")
active = test()
assert active == ["__test1"]
assert mdp.get_active_extensions() == ["__test1"]
示例6: testExtensionInheritanceInjectionNonExtension
# 需要导入模块: import py [as 别名]
# 或者: from py import test [as 别名]
def testExtensionInheritanceInjectionNonExtension():
"""Test non_extension method injection."""
class TestExtensionNode(mdp.ExtensionNode):
extension_name = "__test"
def _execute(self):
return 0
class TestNode(mdp.Node):
# no _execute method
pass
class ExtendedTestNode(TestExtensionNode, TestNode):
pass
test_node = TestNode()
mdp.activate_extension('__test')
assert hasattr(test_node, "_non_extension__execute")
mdp.deactivate_extension('__test')
assert not hasattr(test_node, "_non_extension__execute")
assert not hasattr(test_node, "_extension_for__execute")
# test that the non-native _execute has been completely removed
assert "_execute" not in test_node.__class__.__dict__
示例7: testExtensionInheritanceInjectionNonExtension2
# 需要导入模块: import py [as 别名]
# 或者: from py import test [as 别名]
def testExtensionInheritanceInjectionNonExtension2():
"""Test non_extension method injection."""
class TestExtensionNode(mdp.ExtensionNode):
extension_name = "__test"
def _execute(self):
return 0
class TestNode(mdp.Node):
def _execute(self):
return 1
class ExtendedTestNode(TestExtensionNode, TestNode):
pass
test_node = TestNode()
mdp.activate_extension('__test')
# test that non-extended attribute has been added as well
assert hasattr(test_node, "_non_extension__execute")
mdp.deactivate_extension('__test')
assert not hasattr(test_node, "_non_extension__execute")
assert not hasattr(test_node, "_extension_for__execute")
# test that the native _execute has been preserved
assert "_execute" in test_node.__class__.__dict__
示例8: test_QuadraticForm_extrema
# 需要导入模块: import py [as 别名]
# 或者: from py import test [as 别名]
def test_QuadraticForm_extrema():
# TODO: add some real test
# check H with negligible linear term
noise = 1e-8
tol = 1e-6
x = numx_rand.random((10,))
H = numx.outer(x, x) + numx.eye(10)*0.1
f = noise*numx_rand.random((10,))
q = utils.QuadraticForm(H, f)
xmax, xmin = q.get_extrema(utils.norm2(x), tol=tol)
assert_array_almost_equal(x, xmax, 5)
# check I + linear term
H = numx.eye(10, dtype='d')
f = x
q = utils.QuadraticForm(H, f=f)
xmax, xmin = q.get_extrema(utils.norm2(x), tol=tol)
assert_array_almost_equal(f, xmax, 5)
示例9: test_nested_import_error
# 需要导入模块: import py [as 别名]
# 或者: from py import test [as 别名]
def test_nested_import_error(self, testdir):
p = testdir.makepyfile(
"""
import import_fails
def test_this():
assert import_fails.a == 1
"""
)
testdir.makepyfile(import_fails="import does_not_work")
result = testdir.runpytest(p)
result.stdout.fnmatch_lines(
[
"ImportError while importing test module*",
"*No module named *does_not_work*",
]
)
assert result.ret == 2
示例10: test_namespace_import_doesnt_confuse_import_hook
# 需要导入模块: import py [as 别名]
# 或者: from py import test [as 别名]
def test_namespace_import_doesnt_confuse_import_hook(self, testdir):
"""
Ref #383. Python 3.3's namespace package messed with our import hooks
Importing a module that didn't exist, even if the ImportError was
gracefully handled, would make our test crash.
Use recwarn here to silence this warning in Python 2.7:
ImportWarning: Not importing directory '...\not_a_package': missing __init__.py
"""
testdir.mkdir("not_a_package")
p = testdir.makepyfile(
"""
try:
from not_a_package import doesnt_exist
except ImportError:
# We handle the import error gracefully here
pass
def test_whatever():
pass
"""
)
res = testdir.runpytest(p.basename)
assert res.ret == 0
示例11: test_import_star_py_dot_test
# 需要导入模块: import py [as 别名]
# 或者: from py import test [as 别名]
def test_import_star_py_dot_test(self, testdir):
p = testdir.makepyfile(
"""
from py.test import *
#collect
#cmdline
#Item
# assert collect.Item is Item
# assert collect.Collector is Collector
main
skip
xfail
"""
)
result = testdir.runpython(p)
assert result.ret == 0
示例12: test_fixture_order_respects_scope
# 需要导入模块: import py [as 别名]
# 或者: from py import test [as 别名]
def test_fixture_order_respects_scope(testdir):
"""Ensure that fixtures are created according to scope order, regression test for #2405
"""
testdir.makepyfile(
"""
import pytest
data = {}
@pytest.fixture(scope='module')
def clean_data():
data.clear()
@pytest.fixture(autouse=True)
def add_data():
data.update(value=True)
@pytest.mark.usefixtures('clean_data')
def test_value():
assert data.get('value')
"""
)
result = testdir.runpytest()
assert result.ret == 0
示例13: test_logfinish_hook
# 需要导入模块: import py [as 别名]
# 或者: from py import test [as 别名]
def test_logfinish_hook(self, testdir):
"""Ensure the pytest_runtest_logfinish hook is being properly handled"""
from _pytest import hookspec
if not hasattr(hookspec, "pytest_runtest_logfinish"):
pytest.skip("test requires pytest_runtest_logfinish hook in pytest (3.4+)")
testdir.makeconftest(
"""
def pytest_runtest_logfinish():
print('pytest_runtest_logfinish hook called')
"""
)
testdir.makepyfile(
"""
def test_func():
pass
"""
)
result = testdir.runpytest("-n1", "-s")
result.stdout.fnmatch_lines(["*pytest_runtest_logfinish hook called*"])
示例14: test_config_initialization
# 需要导入模块: import py [as 别名]
# 或者: from py import test [as 别名]
def test_config_initialization(testdir, pytestconfig):
"""Ensure workers and master are initialized consistently. Integration test for #445"""
if not hasattr(pytestconfig, "invocation_params"):
pytest.skip(
"requires pytest >=5.1 (config has no attribute 'invocation_params')"
)
testdir.makepyfile(
**{
"dir_a/test_foo.py": """
def test_1(): pass
"""
}
)
testdir.makefile(
".ini",
myconfig="""
[pytest]
testpaths=dir_a
""",
)
result = testdir.runpytest("-n2", "-c", "myconfig.ini", "-v")
result.stdout.fnmatch_lines(["dir_a/test_foo.py::test_1*"])
示例15: test_issue_594_random_parametrize
# 需要导入模块: import py [as 别名]
# 或者: from py import test [as 别名]
def test_issue_594_random_parametrize(testdir):
"""
Make sure that tests that are randomly parametrized display an appropriate
error message, instead of silently skipping the entire test run.
"""
p1 = testdir.makepyfile(
"""
import pytest
import random
xs = list(range(10))
random.shuffle(xs)
@pytest.mark.parametrize('x', xs)
def test_foo(x):
assert 1
"""
)
result = testdir.runpytest(p1, "-v", "-n4")
assert result.ret == 1
result.stdout.fnmatch_lines(["Different tests were collected between gw* and gw*"])