Pytest Teardown Fixture - python

I need to delete some folders created for testing.
In the main test folder, I created the a file test_teardown.py with the following content.
import shutil
import pytest
#pytest.fixture(scope="session")
def teardown():
yield
shutil.rmtree('tmp')
Yet, the tmp folder is not deleted after the testing session is completed. Am I using the fixture incorrectly?
File structure
+-- Project folder
| +-- tests
| | +-- __init__.py
| | +-- test_teardown.py
| | +-- Unit
| | | +-- __init__.py
| | | +-- test_moretests.py

Thanks to #MrBean Bremen
Adding autouse=True to the wrapper ensures that it's automatically involved.
Do make sure that your test_teardown.py is included in the used test folder.
Wrapper:
#pytest.fixture(scope='session', autouse=True)
def teardown():
yield
shutil.rmtree('tmp')

Related

relative paths in python not finding parent package

I have a file system like this at the moment.
app
|--__init__.py (empty)
|
|--domain
| |--__init__.py (empty)
| |--model.py
| |--questionmatcher.py
|
|--interface
| |--__init__.py (empty)
| |--basiccli.py
| |--userinterface.py
|
|--parser
| |--__init__.py (empty)
| |--json_loader.py
| |--parsing.py
|
|--testfiles
| |--__init__.py (empty)
| |--testsuite.py
I am trying to run the testsuite.py which will need to import classes from various files in directory.
I have tried this structure:
import unittest
from ..parser.json_loader import JsonLoader
from ..parser.parsing import get_vectors, parseThreadsFromFile, getPostsFromThreads
from ..domain import UniversalEncoder, SentBERT
class TestParsing(unittest.TestCase):
def test(self):
pass
class TestJson(unittest.TestCase):
def test(self):
pass
class TestModelEncoders(unittest.TestCase):
def test(self):
pass
if __name__ == "__main__":
unittest.main()
However when I go to run the test I get:
from ..parser.json_loader import JsonLoader
ImportError: attempted relative import with no known parent package
EDIT:
I have tried
from parser.json_loader import JsonLoader
but now I get
from parser.json_loader import JsonLoader
ModuleNotFoundError: No module named 'parser.json_loader'; 'parser' is not a package
you can add this package to your PYTHONPATH environmental variable:
export PYTHONPATH=$PYTHONPATH:/path/to/parser

Pytest - ModuleNotFoundError: No module named 'x'

****solved: added __init__.py to Test/ and renamed testcode.py to test_code.py. To run tests cd -> Zee and type pytest****
Structure:
|--Zee/
| |--Test/
| | |--__init__.py
| | |--test_code.py
| |--Codetotest/
| | |--code.py
in code.py
class Foo():
some code...
in testcode.py
from Codetotest.code import Foo
def test_foo():
assert ...
When I move to the Zee directory in my command line and run pytest Test/testcode.py I get ModuleNotFoundError: No module named Zee. How can I fix this?
I tried making Test a module by adding Test/__init__.py as suggested here. Ran from multiple directories, no dice.
Pytest version 5.3.4, imported from python 3.6
What I don't understand is, when I add __init__.py to Zee/, it gives me the same error
You need a __init__.py in the module directory.
Here's a typical project structure:
|--zee-project-directory/
| |--tests/
| | |--test_zee.py
| |--zee/
| | |--__init__.py
| | |--code.py
code.py
class Foo():
some code...
test_zee.py
from zee.code import Foo
def test_foo():
assert ...

pytest configuration problem (transition from nosetests (71 sec) to pytest (1536 sec))

The problem:
pytest (decided by policy) takes 1536 seconds to run the same test suite (585 tests) as nosetest, which runs in 71 seconds.
The pytest.ini files is:
[pytest]
python_files = tests_*.py *_tests.py
norecursedirs = .idea (pycharm env).
testpaths = tests
And the file is placed at the root of the project:
root
|-+ mod1
| |-- core.py
| |-- utils.py
|-+ mod2
| |-- core.py
| |-- utils2.py
|-+ tests
| |-- test_mod1
| |-- test_mod2
|-+ utils (don't test).
| |-- u1.py
| |-- u2.py
|- pytest.ini
|- readme.md
Things I've checked (following advice from the 14 other SO posts):
The number of Pass/Fails is the same.
When running the tests individually with pytests they take ~ 20ms.
When running the folder with pytests 10-20 tests take 14-15 seconds.
The test suite has one environment, there's no env or os magic. Just lots of technical logic.
Each test_xyz.py file has it's own isolated def setup and def teardown that creates/drop an sqlite database. The tests interact with the database, by adding new transactions and checking the additions. Example:
global db
def setup():
db = get_new_db()
def teardown():
pass
def test_01():
w = Widget(db) # create widget instance.
w.add_friend('a#b.com')
assert 'a#b.com' in w.friends()
Questions:
Do I really have to plaster #pytest.fixtures(scope='module') on the setup and teardown of every 585 tests? I hope not.
What can I do to get the runtime of pytest to be similar to nosetests?
I'm not sure why pytest chose to invoke the module setup function in a pytest_runtest_setup hook that runs once per each test instead of a module-scoped autouse fixture, but here it is:
#hookimpl(trylast=True)
def pytest_runtest_setup(item):
if is_potential_nosetest(item):
if not call_optional(item.obj, "setup"):
# call module level setup if there is no object level one
call_optional(item.parent.obj, "setup")
# XXX this implies we only call teardown when setup worked
item.session._setupstate.addfinalizer((lambda: teardown_nose(item)), item)
This means you'll need to rename the setup/teardown functions to setup_module()/teardown_module(). If you're on Linux/MacOS, you can use sed combined with grep for batch renaming:
$ grep -lr "\(def setup():\|def teardown():\)" | \
xargs sed -i 's/def setup():/def setup_module():/g;s/def teardown():/def teardown_module():/g'

Circular dependency in Python how to solve it?

Here's the structure of my project:
MyProject
|
|---- package1
| |
| |---- classA needs classXYZ
| |-----classB needs classXYZ
| |-----classC
|
|-----package2
| |
| |-----classXYZ (path creator)
|
|-----package3
| |
| |-----classQ (subclass of classR)
| |-----classR
|
|-----package4
| |
| |-----classDB needs classXYZ
ClassQ needs classA, classB, classR, classDB and classXYZ, So, in classQ I have:
from package1 import classA
from package1 import classB
from package2 import classXYZ
from package3 import classR
from package4 import classDB
However, both class classA and classB use classXYZ which results in fact that in classQ I'm getting the error: Import Error: no module named classR. (Class Q inherits from class R).
The question is: how to solve this? Class Q is some kind of main class, which uses functions available in other modules to make a working application. Any help will be appreciated.

How to document Matlab project with multiple classes using sphinxcontrib-matlabdomain?

I tried to document a larger Matlab project using Sphinx and the sphinxcontrib-matlabdomain package. It works fine for a single folder that stores all .m files. However my project contains multiple classes stored in separate folders such as:
project
|-- matfiles
| |-- #class1
| | |-- class1.m
| | |-- method1.m
| | |-- method2.m
| | +-- method1.m
| |-- #class2
| | |-- class2.m
| | |-- methodA.m
| | |-- methodB.m
| | +-- methodC.m
| +-- function
| |-- fun1.m
| |-- fun2.m
| +-- fun3.m
+-- doc
|-- conf.py
+-- index.rst
I adde the following lines to conf.py:
matlab_src_dir = os.path.abspath('..')
extensions = [
'sphinx.ext.autodoc',
'sphinxcontrib.matlab',
]
primary_domain = 'mat'
And I added the following lines to index.rst:
.. module:: matfiles
.. automethod:: class1.method1
I got the following error:
Exception occurred:
File "/Library/Python/2.7/site-packages/sphinxcontrib/mat_documenters.py", line 788, in import_object
if self.object.attrs.get('Static'):
AttributeError: 'NoneType' object has no attribute 'attrs'
The full traceback has been saved in /var/folders/70/g0lr37wn60gbbymnsks_t5vm0000gn/T/sphinx-err-uD8qpe.log, if you want to report the issue to the developers.
Please also report this if it was a user error, so that a better error message can be provided next time.
A bug report can be filed in the tracker at <https://github.com/sphinx-doc/sphinx/issues>. Thanks!
| reading sources... [100%] index
So my question is, is there a way to document multi class Matlab projects where class methods are saved in a folder with name #class?
I believe this issue has been resolved in the latest version, sphinxcontrib-matlabdomain-0.2.7, which autodocuments class methods with attributes defined in separate files of a #classfolders. Let me know if it's still an issue.

Categories