I'm trying to use a fixture from a subdirectory that is defined in the parent directory's conftest.py however, I'm just constantly getting a "fixture [name] not found" error when doing so. Example of the directory structure:
Tests/
conftest.py
test_1.py
test_subdirectory_1/
test_subdirectory_2/
conftest.py
test_2.py
I'd like to use a fixture that has a session scope in the original conftest.py (in the Tests/ directory) in the test_2.py file. However whenever I run test_2.py from the test_subdirectory_2 folder, it gives me a fixture not found error.
I'm doing this from the command line:
root#[ip]:/Test/test_subdirectory_1/test_subdirectory/2# pytest test_2.py
and it spits out:
E Fixture not found.
however, if I move over to the root directory and I do:
root#[ip]:/Test# pytest -k "test_name_from_test_2"
it works perfectly fine. I need to be able to run it from its own directory by just specifying the file name.
Pytest documentation says this type of file structure should be acceptable as tests are supposed to be able to look upward in parent directories for additional fixtures. Can anyone tell me why this is happening?
Here's the platform/version im using: platform linux -- Python 3.8.5, pytest-6.1.0, py-1.9.0, pluggy-0.13.1
I have a directory structure like what follows:
package_root/lib/package_name/foo.py
in foo.py I have a function that creates a file (bar.py) that contains a function (f). foo.py also has a function that then imports bar and runs bar.f().
When I state, within foo.py "import bar", it works, and I have access to bar.f and it runs just fine.
However, this is not the case when running pytest. We run pytest from package_root and it cannot find the module bar when it attempts to import it. This is because (I believe) when running pytest, it creates bar.py in /package_root which contains no init.py file. Since our tests run automatically for our cicd pipeline, I need it to be able to properly import when running pytest from package_root. Any suggestions?
As far as I comprehend from your question is that you are facing imports issue in your pipeline(Correct me if am wrong). In pytest, normally your framework should contain the pytest.ini/tox.ini along with all the test scripts. Please refer link(http://doc.pytest.org/en/latest/customize.html).
Create a file pytest.ini/tox.ini in your framework design from where you are running your code(in your case in the package_root/ directory).
#pytest.ini
[pytest]
python_paths = . lib/<package-name>/
Suppose I'm writing a test. Obviously it is testing my app, so I need to import the app package somehow into the test script. The directory structure is this:
root/
app/
__init__.py
somemodule.py
tests/
my_test.py
And I run the tests like so:
cd tests
py.test # runs all the tests in the current directory
The question is: How should I import the application modules in my test modules?
In my_test.py, I tried doing from .. import app. This gives me an error Parent module '' not loaded, cannot perform relative import.
What is the standard way to accomplish this?
EDIT: Please note I edited the question to refer specifically to the py.test tool.
You should be able to make it run by properly configuring your py.test.
Add your module to your app/__init__.py the following line
from .somemodule import MyClass # or whatever ur class is called
Create a file called conftest.py in your main folder. You can leave it empty but it is used by py.test to find out the project path. Inside you can run some py.test initialization like adding fixtures.
In your my_test.py you will be able now to call
from app import MyClass
Now from your main folder you can finally:
py.test tests/test.py
This has worked for me. I think py.test has a way to include modules since you are probably not able to achieve the same without it. At least if I did not use py.test I would stick to modifying my PYTHONPATH to point to my application path.
EDIT:
Just to clarify py.test manipylates the sys.path for the testing session to include the root directory. Py.test identify the root path by using the conftest.py file. The root path is then added to the system path and used for testing.
You are indeed able to run:
py.test tests/test.py
and this would also work:
cd..
py.test rootTest/tests/test.py
I am having an issue with nose.
I want to run tests in a tests directory but I don't want nose to change the working directory. This is because I am using relative imports in my library and the unittests themselves.
First the directory structure:
app_dir
app.py
library_dir
tests_dir
test_1.py
test_2.py
I am currently running my tests like this from within the app_dir directory:
python -m unittest discover -s library_dir.tests_dir
This works fine.
Now, I would like to do this with nose (nosetests).
When I try it, I get the following:
nosetests library_dir.tests_dir --collect-only -vv
Failure: SystemError (Parent module '' not loaded, cannot perform relative import) ... ok
Failure: SystemError (Parent module '' not loaded, cannot perform relative import) ... ok
I've looked at similar questions on SO already (there are many) and one suggestion for this issue that I can see is to remove the relative imports from within the unittest itself but I like this pattern. Additionally, if I do change to an absolute import, I get an
(No module named '<the_module_under_test')
Is there any way for nose to find tests from a directory (like what the -w option provides BUT keeping the working directory the same as that from the terminal which invoked nose?
Whoops, looks like all I needed to do was to add __init__.py to the library and the subdirectory tests directory. I was working off of the assumption nose would work as expected with PEP 420
Blockquote
PEP 420: Implicit Namespace Packages
Native support for package directories that don’t require init.py marker files and can automatically span multiple path segments (inspired by various third party approaches to namespace packages, as described in PEP 420)
I'm kind of a rookie with python unit testing, and particularly coverage.py. Is it desirable to have coverage reports include the coverage of your actual test files?
Here's a screenshot of my HTML report as an example.
You can see that the report includes tests/test_credit_card. At first I was trying to omit the tests/ directory from the reports, like so:
coverage html --omit=tests/ -d tests/coverage
I tried several variations of that command but I could not for the life of me get the tests/ excluded. After accepting defeat, I began to wonder if maybe the test files are supposed to be included in the report.
Can anyone shed some light on this?
coverage html --omit="*/test*" -d tests/coverage
Create .coveragerc file in your project root folder, and include the following:
[run]
omit = *tests*
Leaving this here in case if any Django developer needs a .coveragerc for their project.
[run]
source = .
omit = ./venv/*,*tests*,*apps.py,*manage.py,*__init__.py,*migrations*,*asgi*,*wsgi*,*admin.py,*urls.py
[report]
omit = ./venv/*,*tests*,*apps.py,*manage.py,*__init__.py,*migrations*,*asgi*,*wsgi*,*admin.py,*urls.py
Create a file named .coveragerc on your projects root directory, paste the above code and then just run the command:
coverage run manage.py test
In addition, if you want the tests to execute faster run this command instead.
coverage run manage.py test --keepdb --parallel
This will preserve the test DB and will run the tests in parallel.
You can specify the directories you want to exclude by creating a .coveragerc in the project root.
It supports wildcards (which you can use to exclude virtual environment) and comments (very useful for effective tracking).
The below code block shows how omit can be used (taken from the latest documentation) with multiple files and directories.
[run]
omit =
# omit anything in a .local directory anywhere
*/.local/*
# omit everything in /usr
/usr/*
# omit this single file
utils/tirefire.py
In your case, you could have the following in your .coveragerc:
[run]
omit =
# ignore all test cases in tests/
tests/*
For your question on coverage reports, you can think about testing and coverage in the following manner:
When you run pytest or unittest, all the test cases for your source code are executed
When you run coverage, it shows the part of the source code that isn't being used.
When you run pytest with coverage (something like pytest -v --cov), it runs all test cases and shows the part of the source code that isn't being used.
Extra:
You can also specify the location of your HTML report in the configuration file like:
[html]
directory = tests/coverage/html_report/
This is going to create html, js, css, etc. inside tests/coverage/html_report/ everytime you run coverage or pytest -v --cov
You can also explicitly specify which directory has the code you want coverage on instead of saying which things to omit. In a .coveragerc file, if the directory of interest is called demo, this looks like
[run]
source = demo