import decimal
import os
try:
    # Python 2
    from ConfigParser import ConfigParser
except ImportError:
    # Python 3
    from configparser import ConfigParser

import tests.helpers as th
from .helpers import cfgpath, clear_db, get_app_lock, release_app_lock

_parser = ConfigParser({
    'server': 'localhost',
    'username': 'sa',
    'password': '',
    'database': 'tempdb',
    'port': '1433',
    'ipaddress': '127.0.0.1',
    'instance': '',
})

def pytest_addoption(parser):
    parser.addoption(
        "--pymssql-section",
        type="string",
        default=os.environ.get('PYMSSQL_TEST_CONFIG', 'DEFAULT'),
        help="The name of the section to use from tests.cfg"
    )

def pytest_configure(config):
    _parser.read(cfgpath)
    section = config.getoption('--pymssql-section')

    if not _parser.has_section(section) and section != 'DEFAULT':
        raise ValueError('the tests.cfg file does not have section: %s' % section)

    th.config.server = os.getenv('PYMSSQL_TEST_SERVER') or _parser.get(section, 'server')
    th.config.user = os.getenv('PYMSSQL_TEST_USERNAME') or _parser.get(section, 'username')
    th.config.password = os.getenv('PYMSSQL_TEST_PASSWORD') or _parser.get(section, 'password')
    th.config.database = os.getenv('PYMSSQL_TEST_DATABASE') or _parser.get(section, 'database')
    th.config.port = os.getenv('PYMSSQL_TEST_PORT') or _parser.get(section, 'port')
    th.config.ipaddress = os.getenv('PYMSSQL_TEST_IPADDRESS') or _parser.get(section, 'ipaddress')
    th.config.instance = os.getenv('PYMSSQL_TEST_INSTANCE') or _parser.get(section, 'instance')
    th.config.orig_decimal_prec = decimal.getcontext().prec

    get_app_lock()
    clear_db()

def pytest_unconfigure(config):
    release_app_lock()

Add tests/conftest.py for py.test