Created
January 16, 2015 19:17
-
-
Save magixx/6a9764e2737304741654 to your computer and use it in GitHub Desktop.
Modified Fixture setup from test param
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ipdb | |
import pytest | |
from collections import Mapping | |
PARTITION_NAME = 'PARTITION' | |
PARTITION_PASSWORD = 'PASSWORD' | |
@pytest.fixture(scope='class') | |
def computer_resource(request): | |
resource = Computer('Computer') | |
return resource | |
@pytest.fixture(scope='function') | |
def resource_a(request, computer_resource): | |
default_value = {'create': {'partition': PARTITION_NAME, | |
'dafaultchallenge': True, | |
'force': True}, | |
'change_password': {'partition': PARTITION_NAME}, | |
'create_user': {'partition': PARTITION_NAME, | |
'password': PARTITION_PASSWORD, | |
'user_password': PARTITION_PASSWORD}, | |
'lock_user': False | |
} | |
# Overwrite default values | |
if request.scope == 'function' and 'value' in request.funcargnames: | |
update(default_value, request.getfuncargvalue('value'), 2) | |
def fin(): | |
computer_resource.partition = None | |
# do set up | |
if default_value['create']: | |
computer_resource.create(**default_value['create']) | |
if default_value['change_password']: | |
computer_resource.partition.change_password( | |
**default_value['change_password']) | |
if default_value['create_user']: | |
computer_resource.partition.create_user( | |
**default_value['create_user']) | |
if default_value['lock_user']: | |
computer_resource.partition.user_locked = True | |
return computer_resource | |
class TestSomeFixtures(object): | |
updates = {'create': {'partition': 'ABCD'}, | |
'create_user': False, | |
'lock_user': True} | |
def test_regular(self, resource_a): | |
current_resource = resource_a | |
print '\nGot Fixture with resource:\n{}'.format(current_resource) | |
assert current_resource | |
@pytest.mark.parametrize('value', [{'change_password': False, | |
'create_user': False}]) | |
def test_type2(self, resource_a, value): | |
current_resource = resource_a | |
print '\nGot Fixture with resource:\n{}'.format(current_resource) | |
assert current_resource | |
@pytest.mark.parametrize('value', [{'create_user': False}]) | |
def test_type3(self, resource_a, value): | |
current_resource = resource_a | |
print '\nGot Fixture with resource:\n{}'.format(current_resource) | |
assert current_resource | |
@pytest.mark.parametrize('value', [{'create_user': {'user_password': | |
'custom_password'}, | |
'lock_user': True}]) | |
def test_type4(self, resource_a, value): | |
current_resource = resource_a | |
print '\nGot Fixture with resource:\n{}'.format(current_resource) | |
assert current_resource | |
def test_type5(self, resource_a): | |
current_resource = resource_a | |
print '\nGot Fixture with resource:\n{}'.format(current_resource) | |
assert current_resource | |
class Computer(object): | |
def __init__(self, name): | |
self.name = name | |
def __str__(self): | |
return str(vars(self)) | |
def create(self, **kwargs): | |
self.partition = Partition(kwargs.get('partition')) | |
class Partition(object): | |
def __init__(self, name): | |
self.name = name | |
def __str__(self): | |
return str(vars(self)) | |
def __repr__(self): | |
return str(vars(self)) | |
def change_password(self, partition, password='password'): | |
if partition: | |
self.password = password | |
def create_user(self, **kwargs): | |
self.user = 'User 1' | |
self.user_password = kwargs.get('user_password') | |
def update(d, u, depth=-1): | |
""" | |
Recursively merge or update dict-like objects. | |
>>> update({'k1': {'k2': 2}}, {'k1': {'k2': {'k3': 3}}, 'k4': 4}) | |
{'k1': {'k2': {'k3': 3}}, 'k4': 4} | |
""" | |
for k, v in u.iteritems(): | |
if isinstance(v, Mapping) and not depth == 0: | |
r = update(d.get(k, {}), v, depth=max(depth - 1, -1)) | |
d[k] = r | |
elif isinstance(d, Mapping): | |
d[k] = u[k] | |
else: | |
d = {k: u[k]} | |
return d | |
~/workspace/fixture_learning » py.test -v -s -rs -l sandbox.py | |
=================================== test session starts ==================================== | |
platform linux2 -- Python 2.7.6 -- py-1.4.20 -- pytest-2.5.2 -- /usr/bin/python | |
plugins: instafail, xdist | |
collected 5 items | |
sandbox.py:58: TestSomeFixtures.test_regular | |
Got Fixture with resource: | |
{'partition': {'user_password': 'PASSWORD', 'password': 'password', 'name': 'PARTITION', 'user': 'User 1'}, 'name': 'Computer'} | |
PASSED | |
sandbox.py:63: TestSomeFixtures.test_type2[value0] | |
Got Fixture with resource: | |
{'partition': {'name': 'PARTITION'}, 'name': 'Computer'} | |
PASSED | |
sandbox.py:70: TestSomeFixtures.test_type3[value0] | |
Got Fixture with resource: | |
{'partition': {'password': 'password', 'name': 'PARTITION'}, 'name': 'Computer'} | |
PASSED | |
sandbox.py:76: TestSomeFixtures.test_type4[value0] | |
Got Fixture with resource: | |
{'partition': {'user_password': 'custom_password', 'password': 'password', 'name': 'PARTITION', 'user_locked': True, 'user': 'User 1'}, 'name': 'Computer'} | |
PASSED | |
sandbox.py:84: TestSomeFixtures.test_type5 | |
Got Fixture with resource: | |
{'partition': {'user_password': 'PASSWORD', 'password': 'password', 'name': 'PARTITION', 'user': 'User 1'}, 'name': 'Computer'} | |
PASSED | |
================================= 5 passed in 0.23 seconds ================================= |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment