pytest coverage with conditional imports and optional requirements - python

I got a hint to use optional requirements and conditional import to provide a function that can use pandas or not, depending whether it's available.
See here for reference:
https://stackoverflow.com/a/74862141/10576322
This solution works, but if I test this code I get always a bad coverage since I either have pandas imported or not. So even if I configure hatch to create environments for both tests, it looks like the tests don't cover this if/else function definition sufficiently.
Is there a proper way around to eg. combine the two results? Or can I tell coverage that the result is expected for that block of code?
Code
The module is looking like that:
try:
import pandas as pd
PANDAS_INSTALLED = True
except ImportError:
PANDAS_INSTALLED = False
if PANDAS_INSTALLED:
def your_function(...):
# magic with pandas
return output
else:
def your_function(...):
# magic without pandas
return output
The idea is that the two version of the two functions work exactly the same beside the inner procedures. So everybody no matter where can use my_module.my_function and don't need to start writing code depending on what environment they are on.
The same is true for testing. I can write tests for my_module.my_function and if the venv has pandas I am testing one part of it and if not the test is testing the other part.
from mypackage import my_module
def test_my_function:
res = 'foo'
assert my_module.my_function() == res
That is working fine, but coverage evaluation is complicated.
Paths to solution
Till now I am ware of two solutions.
1. mocking the behavior
#TYZ suggested to have always pandad as dependency for testing and mock the global variable.
I tried that, but it didn't work as I expected it. The reason is that I can of course mock the PANDAS_INSTALLED variable, but the function defifintion already took place during import and is not affected anymore by the variable.
I tried to check if I can mock the import in another test module, but didn't succeed.
2. defining venvs w and w/o pandas and combine results
I found that coverage and pytest-cov have the abillity to append test results between environments or combine different results.
In a first test I changed the pytest-cov script in hatch to include --cov-append. That worked, but it's totally global. That means if I get complete coverage in Python 3.8, but for whatever reason the switch doesn't work in Python 3.9 I wouldn't see it.
What I like to do is to combine the different results by some logic inherited from hatchs test.matrix. Like coverage combine py38.core py38.pandas and the same for 3.9. So I would see if I have same coverage for all tested versions.
I guess that there are possibly solutions to do that with tox, but maybe I don't need to include another tool.

If it is a test case you're writing, shouldn't the behavior you're testing be the same regardless of whether pandas is installed or not ? From the original question it appears like you'd have the function defined anyways. The intent of your unit test then ought to be -- "given these parameters test whether return value/behavior is this".
That said, if you want coverage with or without pandas, my recommendation would be to declare 2 differently named functions (which can be imported and unit tested separately), whereas your runtime function is assigned depending on the flag in the import block. Something like:
# your_code.py
try:
import pandas as pd
PANDAS_INSTALLED = True
except ImportError:
PANDAS_INSTALLED = False
def _using_pandas(...):
...
def _not_using_pandas(...):
....
do_something = _using_pandas if PANDAS_INSTALLED else _not_using_pandas
__all__ = ['do_something']
# -------------
# your_tests.py
try:
import pandas as pd
PANDAS_INSTALLED = True
except ImportError:
PANDAS_INSTALLED = False
from your_code import _using_pandas, _not_using_pandas, do_something
import pytest
#pytest.mark.skipif(not PANDAS_INSTALLED)
def test_code_using_pandas(...):
...
#pytest.mark.skipif(PANDAS_INSTALLED)
def test_code_not_using_pandas(...):
...
def test_do_something(...):
# test behavior independent of imports
...

Related

Global function, variable called from different files

I have 2 different files:
One is from CI build:
build.py
ABC_ACTIVATE = False
def activate_abc():
global ABC_ACTIVATE
ABC_ACTIVATE = True
# Maybe some more very long code.
One is from customize
customize.py
from build import *
activate_abc()
print ABC_ACTIVATE
The idea is customize the activation for each environment by 1 function instead of very long code. But it doesn't work since ABC_ACTIVATE is always False.
It seems that the global variable cannot receive the same context in the other file. Potentially some "cyclical dependencies" problem.
So my question is: Is there any better structure solution? The idea is still activate by a function and customize.py would be the last setting for apache build.
The global seem cannot receive the same context in the other files. Maybe some "cyclical dependencies" problem.
Once you imported it, ABC_ACTIVATE becomes local in the context of that script. Therefore, mutating the variable in build.py won't reflect in your other module.
So my question is: Is there any better structure solution?
One thing you could is create an intermediary method that return the ABC_ACTIVATE Boolean in your build.py.
def is_abc_activated():
return ABC_ACTIVATE
and then importing it like so,
from build import activate_abc, is_abc_activated
print(is_abc_activated())
activate_abc()
print(is_abc_activated())
>>>>
False
True
Basically, this will remove your unconditional import from build import * which is an anti-idiom in Python. Also, it will increase readability since accessing ABC_ACTIVATE can be confusing on what exactly you're doing.
After some discussion, my friend found a quite hack solution for it:
build.py:
ABC_ACTIVATE = False
def activate_abc(other_context):
other_context.ABC_ACTIVATE = True
And in customize.py:
from build import *
activate_abc(sys.modules[__name__])
print ABC_ACTIVATE
It works now.
That looks like incorrect syntax for a function definition in build.py: the first { should be a : and the second } is not needed as python uses indentation to signify code blocks
ACTIVATE = False
def activate():
global ACTIVATE
ACTIVATE = True
Maybe you could also do...
import build
build.activate()
...As when the script in build.py uses the variable in the same file whereas your imported variable is a different variable since its being imported to the current file's namespace.

Is there a constant that's True when unittesting, but False otherwise?

In Python's typing module, they have a really helpful constant that's True when type checking, but False otherwise. This means, for example, that you can import classes dynamically if TYPE_CHECKING evaluates to True.
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from module import Class
It would be super useful if unittest had something similar. I can see in the __init__.py file, there exists a variable defined as __unittest = True:
__all__ = ['TestResult', 'TestCase', 'TestSuite',
'TextTestRunner', 'TestLoader', 'FunctionTestCase', 'main',
'defaultTestLoader', 'SkipTest', 'skip', 'skipIf', 'skipUnless',
'expectedFailure', 'TextTestResult', 'installHandler',
__unittest = True
Is there any way to use __unittest in the same way as TYPE_CHECKING from typing?
Reason for this: I have some user examples in my code-base which can be run and plot graphs. I would like to run these examples as part of the unit tests to see when they break and need fixing. I need a dynamic way of stopping the examples trying to open a plotting window and blocking the unit tests, however.
Any help very much appreciated!
Reason for this: I have some user examples in my code-base which can be run and plot graphs. I would like to run these examples as part of the unit tests to see when they break and need fixing. I need a dynamic way of stopping the examples trying to open a plotting window and blocking the unit tests, however.
The best way to achieve that is by mocking. This will replace functionality with some "mocked" functionality. This is generally used for "plotting code" or code that makes "requests" and such like. Because you can disable certain functionality that you don't need/want while testing.
It also avoids cluttering the production code with unittest-related stuff. For type checking this is needed because that happens at runtime but unittests generally don't happen at runtime or should have any effect at runtime.
You haven't said what kind of plotting you used but in case it's matplotlib they do have some documentation to facilitate testing or see this QA.
To check what you are asking (that seems if unittest has been imported) you can check if the key "unittest" is in sys.modules
import sys
import unittest
if "unittest" in sys.modules:
print("I'm unittest-ing")
For situations like this, I'd suggest using command-line arguments to control whether code is run or not.
In this case, you could have a simple module that defines a value like is_unit_testing based on whether the -unittest argument has been passed to the python process.
To handle commandline arguments, please look here: How do I access command line arguments in Python?
import sys
# sys.argv is a list of all commandline arguments you can check through
command_line_arguments_list = sys.argv # e.g. ["arg1", "arg2", "-unittest"]
is_unit_testing = "-unittest" in command_line_arguments_list
You can then pass the arguments as you'd expect via command-line:
python myModule.py arg1 arg2 -unittest
This works well for anything like this where you want multiple 'builds' from the same code base. Such as having a 'no database'/'no gui' mode etc.

Mark test to be run in independent process

I'm using pytest. I have a test which involves checking that an import is not made when something happens. This is easy enough to make, but when the test is run in pytest it gets run in the same process as many other tests, which may import that thing beforehand.
Is there some way to mark a test to be run in its own process? Ideally there'd be some kind of decorator like
#pytest.mark.run_in_isolation
def test_import_not_made():
....
But I haven't found anything like that.
I don't know of a pytest plugin that allows marking a test to run in its own process. The two I'd check are pytest-xdist and ptyest-xprocess (here's a list of pytest plugins), though they don't look like they'll do what you want.
I'd go with a different solution. I assume that the way you're checking whether a module is imported is whether it's in sys.modules. As such, I'd ensure sys.modules doesn't contain the module you're interested in before the test run.
Something like this will ensure sys.modules is in a clean state before your test run.
import sys
#pytest.fixture
def clean_sys_modules():
try:
del sys.modules['yourmodule']
except KeyError:
pass
assert 'yourmodule' not in sys.modules # Sanity check.
#pytest.mark.usefixtures('clean_sys_modules')
def test_foo():
# Do the thing you want NOT to do the import.
assert 'yourmodule' not in sys.modules

Pytest and Dynamic fixture modules

I am writing functional tests using pytest for a software that can run locally and in the cloud. I want to create 2 modules, each with the same module/fixture names, and have pytest load one or the other depending if I'm running tests locally or in the cloud:
/fixtures
/fixtures/__init__.py
/fixtures/local_hybrids
/fixtures/local_hybrids/__init__.py
/fixtures/local_hybrids/foo.py
/fixtures/cloud_hybrids
/fixtures/cloud_hybrids/__init__.py
/fixtures/cloud_hybrids/foo.py
/test_hybrids/test_hybrids.py
foo.py (both of them):
import pytest
#pytest.fixture()
def my_fixture():
return True
/fixtures/__init__.py:
if True:
import local_hybrids as hybrids
else:
import cloud_hybrids as hybrids
/test_hybrids/test_hybrids.py:
from fixtures.hybrids.foo import my_fixture
def test_hybrid(my_fixture):
assert my_fixture
The last code block doesn't work of course, because import fixtures.hybrids is looking at the file system instead of __init__.py's "fake" namespace, which isn't like from fixtures import hybrids, which works (but then you cannot use the fixtures as the names would involve dot notation).
I realize that I could play with pytest_generate_test to alter the fixture dynamically (maybe?) but I'd really hate managing each fixture manually from within that function... I was hoping the dynamic import (if x, import this, else import that) was standard Python, unfortunately it clashes with the fixtures mechanism:
import fixtures
def test(fixtures.hybrids.my_fixture): # of course it doesn't work :)
...
I could also import each fixture function one after the other in init; more legwork, but still a viable option to fool pytest and get fixture names without dots.
Show me the black magic. :) Can it be done?
I think in your case it's better to define a fixture - environment or other nice name.
This fixture can be just a getter from os.environ['KEY'] or you can add custom command line argument like here
then use it like here
and the final use is here.
What im trying to tell is that you need to switch thinking into dependency injection: everything should be a fixture. In your case (and in my plugin as well), runtime environment should be a fixture, which is checked in all other fixtures which depend on the environment.
You might be missing something here: If you want to re-use those fixtures you need to say it explicitly:
from fixtures.hybrids.foo import my_fixture
#pytest.mark.usefixtures('my_fixture')
def test_hybrid(my_fixture):
assert my_fixture
In that case you could tweak pytest as following:
from local_hybrids import local_hybrids_fixture
from cloud_hybrids import cloud_hybrids_fixture
fixtures_to_test = {
"local":None,
"cloud":None
}
#pytest.mark.usefixtures("local_hybrids_fixture")
def test_add_local_fixture(local_hybrids_fixture):
fixtures_to_test["local"] = local_hybrids_fixture
#pytest.mark.usefixtures("cloud_hybrids_fixture")
def test_add_local_fixture(cloud_hybrids_fixture):
fixtures_to_test["cloud"] = cloud_hybrids_fixture
def test_on_fixtures():
if cloud_enabled:
fixture = fixtures_to_test["cloud"]
else:
fixture = fixtures_to_test["local"]
...
If there are better solutions around I am also interested ;)
I don't really think there is a "good way" of doing that in python, but still it is possible with a little amount of hacking. You can update sys.path for the subfolder with fixtures you would like to use and import fixtures directly. In dirty case it look's like that:
for your fixtures/__init__.py:
if True:
import local as hybrids
else:
import cloud as hybrids
def update_path(module):
from sys import path
from os.path import join, pardir, abspath
mod_dir = abspath(join(module.__file__, pardir))
path.insert(0, mod_dir)
update_path(hybrids)
and in the client code (test_hybrids/test_hybrids.py) :
import fixtures
from foo import spam
spam()
In other cases you can use much more complex actions to perform a fake-move of all modules/packages/functions etc from your cloud/local folder directly into the fixture's __init__.py. Still, I think - it does not worth a try.
One more thing - black magic is not the best thing to use, I would recommend you to use a dotted notation with "import X from Y" - this is much more stable solution.
Use the pytest plugins feature and put your fixtures in separate modules. Then at runtime select which plug-in you’ll be drawing from via a command line argument or an environment variable. It needs to be something global because you need to place different pytest_plugins list assignments based on the global value.
Take a look at the section Conditional Plugins from this repo https://github.com/jxramos/pytest_behavior/tree/main/conditional_plugins

Python: intercept a class loading action

Summary: when a certain python module is imported, I want to be able to intercept this action, and instead of loading the required class, I want to load another class of my choice.
Reason: I am working on some legacy code. I need to write some unit test code before I start some enhancement/refactoring. The code imports a certain module which will fail in a unit test setting, however. (Because of database server dependency)
Pseduo Code:
from LegacyDataLoader import load_me_data
...
def do_something():
data = load_me_data()
So, ideally, when python excutes the import line above in a unit test, an alternative class, says MockDataLoader, is loaded instead.
I am still using 2.4.3. I suppose there is an import hook I can manipulate
Edit
Thanks a lot for the answers so far. They are all very helpful.
One particular type of suggestion is about manipulation of PYTHONPATH. It does not work in my case. So I will elaborate my particular situation here.
The original codebase is organised in this way
./dir1/myapp/database/LegacyDataLoader.py
./dir1/myapp/database/Other.py
./dir1/myapp/database/__init__.py
./dir1/myapp/__init__.py
My goal is to enhance the Other class in the Other module. But since it is legacy code, I do not feel comfortable working on it without strapping a test suite around it first.
Now I introduce this unit test code
./unit_test/test.py
The content is simply:
from myapp.database.Other import Other
def test1():
o = Other()
o.do_something()
if __name__ == "__main__":
test1()
When the CI server runs the above test, the test fails. It is because class Other uses LegacyDataLoader, and LegacydataLoader cannot establish database connection to the db server from the CI box.
Now let's add a fake class as suggested:
./unit_test_fake/myapp/database/LegacyDataLoader.py
./unit_test_fake/myapp/database/__init__.py
./unit_test_fake/myapp/__init__.py
Modify the PYTHONPATH to
export PYTHONPATH=unit_test_fake:dir1:unit_test
Now the test fails for another reason
File "unit_test/test.py", line 1, in <module>
from myapp.database.Other import Other
ImportError: No module named Other
It has something to do with the way python resolves classes/attributes in a module
You can intercept import and from ... import statements by defining your own __import__ function and assigning it to __builtin__.__import__ (make sure to save the previous value, since your override will no doubt want to delegate to it; and you'll need to import __builtin__ to get the builtin-objects module).
For example (Py2.4 specific, since that's what you're asking about), save in aim.py the following:
import __builtin__
realimp = __builtin__.__import__
def my_import(name, globals={}, locals={}, fromlist=[]):
print 'importing', name, fromlist
return realimp(name, globals, locals, fromlist)
__builtin__.__import__ = my_import
from os import path
and now:
$ python2.4 aim.py
importing os ('path',)
So this lets you intercept any specific import request you want, and alter the imported module[s] as you wish before you return them -- see the specs here. This is the kind of "hook" you're looking for, right?
There are cleaner ways to do this, but I'll assume that you can't modify the file containing from LegacyDataLoader import load_me_data.
The simplest thing to do is probably to create a new directory called testing_shims, and create LegacyDataLoader.py file in it. In that file, define whatever fake load_me_data you like. When running the unit tests, put testing_shims into your PYTHONPATH environment variable as the first directory. Alternately, you can modify your test runner to insert testing_shims as the first value in sys.path.
This way, your file will be found when importing LegacyDataLoader, and your code will be loaded instead of the real code.
The import statement just grabs stuff from sys.modules if a matching name is found there, so the simplest thing is to make sure you insert your own module into sys.modules under the target name before anything else tries to import the real thing.
# in test code
import sys
import MockDataLoader
sys.modules['LegacyDataLoader'] = MockDataLoader
import module_under_test
There are a handful of variations on the theme, but that basic approach should work fine to do what you describe in the question. A slightly simpler approach would be this, using just a mock function to replace the one in question:
# in test code
import module_under_test
def mock_load_me_data():
# do mock stuff here
module_under_test.load_me_data = mock_load_me_data
That simply replaces the appropriate name right in the module itself, so when you invoke the code under test, presumably do_something() in your question, it calls your mock routine.
Well, if the import fails by raising an exception, you could put it in a try...except loop:
try:
from LegacyDataLoader import load_me_data
except: # put error that occurs here, so as not to mask actual problems
from MockDataLoader import load_me_data
Is that what you're looking for? If it fails, but doesn't raise an exception, you could have it run the unit test with a special command line tag, like --unittest, like this:
import sys
if "--unittest" in sys.argv:
from MockDataLoader import load_me_data
else:
from LegacyDataLoader import load_me_data

Categories

Resources