yuliji
4/19/2017 - 8:15 AM

pytest fixure finalizer

pytest fixure finalizer

import pytest


@pytest.fixture(params=[
    # Tuples with password string and expected result
    ('password', False),
    ('p@ssword', False),
    ('p@ssw0rd', True)
])
def password(request):
    """Password fixture"""
    return request.param


def password_contains_number(password):
    """Checks if a password contains a number"""
    return any([True for x in range(10) if str(x) in password])


def password_contains_symbol(password):
    """Checks if a password contains a symbol"""
    return any([True for x in '!,@,#,$,%,^,&,*,(,),_,-,+,='.split(',') if x in password])


def check_password(password):
    """Check the password"""
    return password_contains_number(password) and password_contains_symbol(password)


def test_password_verifier_works(password):
    """Test that the password is verifyied correctly"""
    (input, result) = password
    print '\n'
    print input

    assert check_password(input) == result
import pytest

# fixures

@pytest.fixture()
def my_fixture():
    print "\nI'm the fixture"

@pytest.mark.usefixtures('my_fixture')
def test_my_fixture():
    print "I'm the test"
    
    
@pytest.mark.usefixtures('my_fixture')
class Test:
    def test1(self):
        print "I'm the test 1"

    def test2(self):
        print "I'm the test 2"
        
import pytest

pytestmark = pytest.mark.usefixtures('my_fixture')

def test_my_fixture():
    print "I'm the test"

class Test:
    def test1(self):
        print "I'm the test 1"

    def test2(self):
        print "I'm the test 2"
@pytest.fixture()
def my_fixture(request):
    data = {'x': 1, 'y': 2, 'z': 3}

    def fin():
        print "\nMic drop"
    request.addfinalizer(fin)

    return data