New-style classes implemented for python 2.7 - #2147

This commit is contained in:
Michal Wajszczuk
2017-02-16 19:41:51 +01:00
parent da828aac05
commit fb0b90646e
61 changed files with 351 additions and 349 deletions

View File

@@ -223,7 +223,7 @@ provides an alternative explanation for ``Foo`` objects::
now, given this test module::
# content of test_foocompare.py
class Foo:
class Foo(object):
def __init__(self, val):
self.val = val

View File

@@ -128,7 +128,7 @@ def test_attribute_multiple():
def globf(x):
return x+1
class TestRaises:
class TestRaises(object):
def test_raises(self):
s = 'qwe'
raises(TypeError, "int(s)")
@@ -167,7 +167,7 @@ def test_dynamic_compile_shows_nicely():
class TestMoreErrors:
class TestMoreErrors(object):
def test_complex_error(self):
def f():
return 44
@@ -213,23 +213,23 @@ class TestMoreErrors:
x = 0
class TestCustomAssertMsg:
class TestCustomAssertMsg(object):
def test_single_line(self):
class A:
class A(object):
a = 1
b = 2
assert A.a == b, "A.a appears not to be b"
def test_multiline(self):
class A:
class A(object):
a = 1
b = 2
assert A.a == b, "A.a appears not to be b\n" \
"or does not appear to be b\none of those"
def test_custom_repr(self):
class JSON:
class JSON(object):
a = 1
def __repr__(self):
return "This is JSON\n{\n 'foo': 'bar'\n}"

View File

@@ -1,7 +1,7 @@
def setup_module(module):
module.TestStateFullThing.classcount = 0
class TestStateFullThing:
class TestStateFullThing(object):
def setup_class(cls):
cls.classcount += 1

View File

@@ -15,7 +15,7 @@ example: specifying and selecting acceptance tests
def pytest_funcarg__accept(request):
return AcceptFixture(request)
class AcceptFixture:
class AcceptFixture(object):
def __init__(self, request):
if not request.config.option.acceptance:
pytest.skip("specify -A to run acceptance tests")
@@ -61,7 +61,7 @@ extend the `accept example`_ by putting this in our test module:
arg.tmpdir.mkdir("special")
return arg
class TestSpecialAcceptance:
class TestSpecialAcceptance(object):
def test_sometest(self, accept):
assert accept.tmpdir.join("special").check()

View File

@@ -7,7 +7,7 @@ def setup(request):
yield setup
setup.finalize()
class CostlySetup:
class CostlySetup(object):
def __init__(self):
import time
print ("performing costly setup")

View File

@@ -21,7 +21,7 @@ You can "mark" a test function with custom metadata like this::
pass
def test_another():
pass
class TestClass:
class TestClass(object):
def test_method(self):
pass
@@ -242,7 +242,7 @@ its test methods::
# content of test_mark_classlevel.py
import pytest
@pytest.mark.webtest
class TestClass:
class TestClass(object):
def test_startup(self):
pass
def test_startup_and_more(self):
@@ -256,14 +256,14 @@ To remain backward-compatible with Python 2.4 you can also set a
import pytest
class TestClass:
class TestClass(object):
pytestmark = pytest.mark.webtest
or if you need to use multiple markers you can use a list::
import pytest
class TestClass:
class TestClass(object):
pytestmark = [pytest.mark.webtest, pytest.mark.slowtest]
You can also set a module level marker::
@@ -407,7 +407,7 @@ code you can read over all such settings. Example::
pytestmark = pytest.mark.glob("module", x=1)
@pytest.mark.glob("class", x=2)
class TestClass:
class TestClass(object):
@pytest.mark.glob("function", x=3)
def test_something(self):
pass

View File

@@ -16,7 +16,7 @@ def python1(request, tmpdir):
def python2(request, python1):
return Python(request.param, python1.picklefile)
class Python:
class Python(object):
def __init__(self, version, picklefile):
self.pythonpath = py.path.local.sysfind(version)
if not self.pythonpath:

View File

@@ -168,7 +168,7 @@ only have to work a bit to construct the correct arguments for pytest's
scenario1 = ('basic', {'attribute': 'value'})
scenario2 = ('advanced', {'attribute': 'value2'})
class TestSampleWithScenarios:
class TestSampleWithScenarios(object):
scenarios = [scenario1, scenario2]
def test_demo1(self, attribute):
@@ -241,9 +241,9 @@ creates a database object for the actual test invocations::
if 'db' in metafunc.fixturenames:
metafunc.parametrize("db", ['d1', 'd2'], indirect=True)
class DB1:
class DB1(object):
"one database object"
class DB2:
class DB2(object):
"alternative database object"
@pytest.fixture
@@ -350,7 +350,7 @@ parametrizer`_ but in a lot less code::
metafunc.parametrize(argnames, [[funcargs[name] for name in argnames]
for funcargs in funcarglist])
class TestClass:
class TestClass(object):
# a map specifying multiple argument sets for a test method
params = {
'test_equals': [dict(a=1, b=2), dict(a=3, b=3), ],

View File

@@ -4,7 +4,7 @@
def test_function():
pass
class TestClass:
class TestClass(object):
def test_method(self):
pass
def test_anothermethod(self):

View File

@@ -107,7 +107,7 @@ This would make ``pytest`` look for tests in files that match the ``check_*
that match ``*_check``. For example, if we have::
# content of check_myapp.py
class CheckMyApp:
class CheckMyApp(object):
def simple_check(self):
pass
def complex_check(self):

View File

@@ -550,7 +550,7 @@ get on the terminal - we are working on that)::
self = <failure_demo.TestCustomAssertMsg object at 0xdeadbeef>
def test_single_line(self):
class A:
class A(object):
a = 1
b = 2
> assert A.a == b, "A.a appears not to be b"
@@ -564,7 +564,7 @@ get on the terminal - we are working on that)::
self = <failure_demo.TestCustomAssertMsg object at 0xdeadbeef>
def test_multiline(self):
class A:
class A(object):
a = 1
b = 2
> assert A.a == b, "A.a appears not to be b\n" \
@@ -581,7 +581,7 @@ get on the terminal - we are working on that)::
self = <failure_demo.TestCustomAssertMsg object at 0xdeadbeef>
def test_custom_repr(self):
class JSON:
class JSON(object):
a = 1
def __repr__(self):
return "This is JSON\n{\n 'foo': 'bar'\n}"

View File

@@ -425,7 +425,7 @@ tests in a class. Here is a test module example:
import pytest
@pytest.mark.incremental
class TestUserHandling:
class TestUserHandling(object):
def test_login(self):
pass
def test_modification(self):
@@ -483,7 +483,7 @@ Here is an example for making a ``db`` fixture available in a directory:
# content of a/conftest.py
import pytest
class DB:
class DB(object):
pass
@pytest.fixture(scope="session")

View File

@@ -28,7 +28,7 @@ will be called ahead of running any tests::
# content of test_module.py
class TestHello:
class TestHello(object):
@classmethod
def callme(cls):
print ("callme called!")
@@ -39,7 +39,7 @@ will be called ahead of running any tests::
def test_method2(self):
print ("test_method1 called")
class TestOther:
class TestOther(object):
@classmethod
def callme(cls):
print ("callme other called")

View File

@@ -557,7 +557,7 @@ and instantiate an object ``app`` where we stick the already defined
import pytest
class App:
class App(object):
def __init__(self, smtp):
self.smtp = smtp
@@ -728,7 +728,7 @@ and declare its use in a test module via a ``usefixtures`` marker::
import pytest
@pytest.mark.usefixtures("cleandir")
class TestDirectoryInit:
class TestDirectoryInit(object):
def test_cwd_starts_empty(self):
assert os.listdir(os.getcwd()) == []
with open("myfile", "w") as f:
@@ -791,7 +791,7 @@ self-contained implementation of this idea::
import pytest
class DB:
class DB(object):
def __init__(self):
self.intransaction = []
def begin(self, name):
@@ -803,7 +803,7 @@ self-contained implementation of this idea::
def db():
return DB()
class TestClass:
class TestClass(object):
@pytest.fixture(autouse=True)
def transact(self, request, db):
db.begin(request.function.__name__)
@@ -861,7 +861,7 @@ into a conftest.py file **without** using ``autouse``::
and then e.g. have a TestClass using it by declaring the need::
@pytest.mark.usefixtures("transact")
class TestClass:
class TestClass(object):
def test_method1(self):
...

View File

@@ -24,7 +24,7 @@ resources. Here is a basic example how we could implement
a per-session Database object::
# content of conftest.py
class Database:
class Database(object):
def __init__(self):
print ("database instance created")
def destroy(self):

View File

@@ -1,7 +1,7 @@
import textwrap
import inspect
class Writer:
class Writer(object):
def __init__(self, clsname):
self.clsname = clsname

View File

@@ -111,7 +111,7 @@ to group tests logically, in classes and modules. Let's write a class
containing two tests::
# content of test_class.py
class TestClass:
class TestClass(object):
def test_one(self):
x = "this"
assert 'h' in x

View File

@@ -98,7 +98,7 @@ You can use the ``skipif`` decorator (and any other marker) on classes::
@pytest.mark.skipif(sys.platform == 'win32',
reason="does not run on windows")
class TestPosixCalls:
class TestPosixCalls(object):
def test_function(self):
"will not be setup or run under 'win32' platform"

View File

@@ -110,7 +110,7 @@ If you want to disable a complete test class you
can set the class-level attribute ``disabled``.
For example, in order to avoid running some tests on Win32::
class TestPosixOnly:
class TestPosixOnly(object):
disabled = sys.platform == 'win32'
def test_xxx(self):

View File

@@ -71,7 +71,7 @@ it from a unittest-style test::
@pytest.fixture(scope="class")
def db_class(request):
class DummyDB:
class DummyDB(object):
pass
# set a class attribute on the invoking test context
request.cls.db = DummyDB()

View File

@@ -226,7 +226,7 @@ to all testcases you can use ``LogXML.add_global_properties``
def start_and_prepare_env():
pass
class TestMe:
class TestMe(object):
def test_foo(self):
assert True
@@ -314,7 +314,7 @@ You can specify additional plugins to ``pytest.main``::
# content of myinvoke.py
import pytest
class MyPlugin:
class MyPlugin(object):
def pytest_sessionfinish(self):
print("*** test run reporting finishing")