AIM - I am trying to pass a config variable 'db_str' to my pytest script (test_script.py)
The db_str variable is defined in development.ini
I have tried using command
pytest -c development.ini regression_tests/test_script.py
But it didn't work
Error
> conn_string = config['db_string']
KeyError: 'db_string'
I tried using conftest.py, but didn't work
#contest.py code
import pytest
def pytest_addoption(parser):
parser.addoption("--set-db_st",
action="store",help="host='localhost' dbname='xyz' user='portaladmin'")
#pytest.fixture
def db_str(request):
return request.config.getoption("--set-db_str")
Pytest code
from S4M_pyramid.modelimport MyModel
from S4M_pyramid.lib.deprecated_pylons_globals import config
import subprocess
config['db_str'] = db_str
def test_get_dataset_mapping_id():
result = MyModel.get_dataset_mapping_id()
assert len(result) >1
How can I pass variable 'db_str' from development.ini or any other ini file to pytest script
The logic is the fillowing:
Define CLI argument that will be used to pass information about environment/config file
Get CLI argument value in pytest fixture
Parse config file
Use parsed config in get_database_string fixture to get database connection string
Use get_database_string fixture in your tests to get connection string
conftest.py
import os
from configparser import ConfigParser
# in root of the project there is file project_paths.py
# with the following code ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
import project_paths
def pytest_addoption(parser):
"""Pytest hook that defines list of CLI arguments with descriptions and default values
:param parser: pytest specific argument
:return: void
"""
parser.addoption('--env', action='store', default='development',
help='setup environment: development')
#pytest.fixture(scope="function")
def get_database_string(get_config):
"""Fixture that returns db_string
:param get_config: fixture that returns ConfigParser object that access
to config file
:type: ConfigParser
:return: Returns database connection string
:rtype: str
"""
return get_config['<section name>']['db_string']
#pytest.fixture(scope="function")
def get_config(request):
"""Functions that reads and return ConfigParser object that access
to config file
:rtype: ConfigParser
"""
environment = request.config.getoption("--env")
config_parser = ConfigParser()
file_path = os.path.join(project_paths.ROOT_DIR, '{}.ini'.format(environment))
config_parser.read(file_path)
return config_parser
test_file.py
import pytest
def test_function(get_database_string)
print(get_database_string)
>>> <data base string from development.ini>
As described on pytest_addoption:
add options:
To add command line options, call parser.addoption(...).
To add ini-file values call parser.addini(...).
get options:
Options can later be accessed through the config object, respectively:
config.getoption(name) to retrieve the value of a command line option.
config.getini(name) to retrieve a value read from an ini-style file.
conftest.py:
def pytest_addoption(parser):
parser.addini('foo', '')
test.py:
def test_func(request):
request.config.getini('foo')
Related
On databricks, I have a notebook of code and a notebook of unit tests.
The code is "imported" into the unit test notebook using the "%run" command.
How can I make a mock object of one of the functions in the code notebook from the unit test notebook? I'd typically use the patch context manager for this.
Here is the code notebook with the function to be patched (get_name):
# Databricks notebook source
def get_name_func():
return 'name1'
Here is the unit test code:
# Databricks notebook source:
from unittest.mock import patch
import inspect
# COMMAND ----------
# MAGIC %run ./get_name
# COMMAND ----------
def local_get_name():
return 'name_local'
# COMMAND ----------
get_name_func()
# COMMAND ----------
print(inspect.getmodule(get_name_func))
print(inspect.getsourcefile(get_name_func))
# COMMAND ----------
inspect.unwrap(get_name_func)
# COMMAND ----------
with patch('get_name_func') as mock_func:
print(mock_func)
# COMMAND ----------
with patch('local_get_name') as mock_func:
print(mock_func)
Both patch attempts, for the local function and the function in the code notebook, give the same error:
TypeError: Need a valid target to patch. You supplied: 'get_name_func'
The inspect commands return:
<module '__main__' from '/local_disk0/tmp/1625490167313-0/PythonShell.py'>
<command-6807918>
and
Out[38]: <function __main__.get_name_func()>
I've tried various combinations for the module path with no luck.
Strangely, __name__ returns '__main__'. But using the path '__main__.get_name_func' in the patch call does not work.
My belief is that if the object exists in the notebook (which it definitely does), then it must be patchable.
Any suggestions?
I had to make my own patching function:
class FunctionPatch():
'''
This class is a context manager that allows patching of functions "imported" from another notebook using %run.
The patch function must be at global scope (i.e. top level)
'''
def __init__(self, real_func_name: str, patch_func: Callable):
self._real_func_name = real_func_name
self._patch_func = patch_func
self._backup_real_func = None
def __enter__(self):
self._backup_real_func = globals()[self._real_func_name]
globals()[self._real_func_name] = self._patch_func
def __exit__(self, exc_type, exc_value, tb):
if exc_type is not None:
traceback.print_exception(exc_type, exc_value, tb)
globals()[self._real_func_name] = self._backup_real_func
Usage:
def test_function_patch_real_func():
return 'real1'
def test_function_patch():
assert test_function_patch_real_func() == 'real1'
def mock_func():
return 'mock1'
with FunctionPatch('test_function_patch_real_func', mock_func):
assert test_function_patch_real_func() == 'mock1'
assert test_function_patch_real_func() == 'real1'
I've written some python code that needs to read a config file at /etc/myapp/config.conf . I want to write a unit test for what happens if that file isn't there, or contains bad values, the usual stuff. Lets say it looks like this...
""" myapp.py
"""
def readconf()
""" Returns string of values read from file
"""
s = ''
with open('/etc/myapp/config.conf', 'r') as f:
s = f.read()
return s
And then I have other code that parses s for its values.
Can I, through some magic Python functionality, make any calls that readconf makes to open redirect to custom locations that I set as part of my test environment?
Example would be:
main.py
def _open_file(path):
with open(path, 'r') as f:
return f.read()
def foo():
return _open_file("/sys/conf")
test.py
from unittest.mock import patch
from main import foo
def test_when_file_not_found():
with patch('main._open_file') as mopen_file:
# Setup mock to raise the error u want
mopen_file.side_effect = FileNotFoundError()
# Run actual function
result = foo()
# Assert if result is expected
assert result == "Sorry, missing file"
Instead of hard-coding the config file, you can externalize it or parameterize it. There are 2 ways to do it:
Environment variables: Use a $CONFIG environment variable that contains the location of the config file. You can run the test with an environment variable that can be set using os.environ['CONFIG'].
CLI params: Initialize the module with commandline params. For tests, you can set sys.argv and let the config property be set by that.
In order to mock just calls to open in your function, while not replacing the call with a helper function, as in Nf4r's answer, you can use a custom patch context manager:
from contextlib import contextmanager
from types import CodeType
#contextmanager
def patch_call(func, call, replacement):
fn_code = func.__code__
try:
func.__code__ = CodeType(
fn_code.co_argcount,
fn_code.co_kwonlyargcount,
fn_code.co_nlocals,
fn_code.co_stacksize,
fn_code.co_flags,
fn_code.co_code,
fn_code.co_consts,
tuple(
replacement if call == name else name
for name in fn_code.co_names
),
fn_code.co_varnames,
fn_code.co_filename,
fn_code.co_name,
fn_code.co_firstlineno,
fn_code.co_lnotab,
fn_code.co_freevars,
fn_code.co_cellvars,
)
yield
finally:
func.__code__ = fn_code
Now you can patch your function:
def patched_open(*args):
raise FileNotFoundError
with patch_call(readconf, "open", "patched_open"):
...
You can use mock to patch a module's instance of the 'open' built-in to redirect to a custom function.
""" myapp.py
"""
def readconf():
s = ''
with open('./config.conf', 'r') as f:
s = f.read()
return s
""" test_myapp.py
"""
import unittest
from unittest import mock
import myapp
def my_open(path, mode):
return open('asdf', mode)
class TestSystem(unittest.TestCase):
#mock.patch('myapp.open', my_open)
def test_config_not_found(self):
try:
result = myapp.readconf()
assert(False)
except FileNotFoundError as e:
assert(True)
if __name__ == '__main__':
unittest.main()
You could also do it with a lambda like this, if you wanted to avoid declaring another function.
#mock.patch('myapp.open', lambda path, mode: open('asdf', mode))
def test_config_not_found(self):
...
I am parsing a config.ini file using python ConfigParser, but if my config file does not have a specific item then there is an error. I want to have a check in place so that if i try to get a value which is not in the config file the that should return None.
Below is the config parser class that i have written.
from configparser import ConfigParser
class MyConfiguration(object):
def __init__(self, *file_names):
# print(file_names)
parser = ConfigParser()
# parser.optionxform = str
found = parser.read(file_names)
if not found:
raise ValueError('No config file found!')
for section in parser.sections():
# print(section)
# print(parser.items(section))
self.__dict__.update(parser.items(section))
config = MyConfiguration(FILE1, FILE2)
to use any item from config file I am calling config.key
Expectation:
if I try to get config.key_unknown which is not available it should return None
Instead of setting keys in the dictionary, you should probably use __getattr__:
from configparser import ConfigParser
class MyConfiguration(object):
def __init__(self, *file_names):
# print(file_names)
parser = ConfigParser()
# parser.optionxform = str
found = parser.read(file_names)
if not found:
raise ValueError('No config file found!')
self._parser = parser
def __getattr__(self, key):
for section in self._parser.sections():
if key in section:
return section[key]
return None
I have the following class file and a corresponding test file
dir.py:
import os
class Dir:
def __init__(self, path=''):
self.path = path
#property
def path(self):
return self._path
#path.setter
def path(self, path):
abspath = os.path.abspath(path)
if abspath.exists():
self._path = path
else:
raise IOError(f'{path} does not exist')
and dir_test.py:
import unittest
from ..dir import Dir
class TestDir(unittest.TestCase):
def IOErrorIfPathNotExists(self):
with self.assertRaises(IOError):
Dir.path = "~/invalidpath/"
with self.assertRaises(IOError):
Dir('~/invalidpath/')
if __name__ == "__main__":
unittest.main()
but when I run
pytest -x dir_test.py
it just prints no tests ran in 0.01 seconds
and I have no idea why. It is my first time using pytest except with exercises from exercism.io, and I can't spot any difference to their test files.
I am running it in a virtual environment (Python 3.6.5), with pytest and pytest-cache installed via pip.
That's because your test method is not named properly.
By default, pytest will consider any class prefixed with Test as a test collection.
Yours is TestDir, this matches.
By default, pytest will consider any function prefixed with test as a test.
Yours is IOErrorIfPathNotExists, which does not start with test and is not executed.
Source.
Have defined Cases.py file with defined suite:
import unittest
import pytest
from adminzone_tests.Clients import TestClients
def collect_suite():
suite = unittest.TestSuite()
suite.addTest(TestClients)
return suite
#pytest.mark.usefixtures('admin_session')
def run():
unittest.TextTestRunner(verbosity=2).run(collect_suite())
#pytest.mark.usefixtures('client_session')
def run():
unittest.TextTestRunner(verbosity=2).run(collect_suite())
conftest.py file:
import pytest
from base.Common import Common
from base.Users import Users
#pytest.fixture(scope='session')
def admin_session(request):
Users.users('admin')
def admin_session_teardown():
Common.logout()
request.addfinalizer(admin_session_teardown)
#pytest.fixture(scope='session')
def client_session(request):
Users.users('client')
def client_session_teardown():
Common.logout()
request.addfinalizer(client_session_teardown)
Idea: execute the same suite one by one with different user's sessions.
But fixture executes only if has param autouse=True
How to make fixture executable without autouse=True and run the same suite with different setup ?
Finally i found solution that i was looking for, could be useful for somebody:
Used hook with command line args
conftest.py file:
import pytest
def pytest_addoption(parser):
# adding command line arg --user
parser.addoption('--users', action='append', default=['user1'])
def pytest_generate_tests(metafunc):
if 'users' in metafunc.fixturenames:
us = set(metafunc.config.option.users)
if not us:
us = ['user1']
elif 'all' in us:
us = ['user2', 'user1']
else:
us = list(us)
metafunc.parametrize('users', us, scope='session')
#pytest.yield_fixture(scope='session', autouse=True)
def define_users(request, users):
# Execute login here, as example called function with param of user_type
login(users)
def teardown():
# Execute logout here
logout()
request.addfinalizer(teardown)
Cases.py file:
import unittest
suite = unittest.TestSuite()
def collect_suite():
suite.addTest(TestClass1)
# adding more test classes here
def run():
unittest.TextTestRunner(verbosity=2).run(suite)
Now via command line execute command(in project's root dir)
python -m pytest Cases.py -s -v -n auto --users all
Test suite will be executed with login as user1, after will be executed with login as user2