Testdriven.io: Scalable FastAPI Applications on AWS. pytest not found main module - import

while i try to this course https://testdriven.io/courses/scalable-fastapi-aws/
i have some problems.
when i type
talk-booking/services/talk_booking $ poetry run pytest tests/integration
error raises
=============================================== test session starts ================================================
platform darwin -- Python 3.11.1, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /Users/blarblar/Workspace/talk-booking/services/talk_booking
plugins: cov-4.0.0, anyio-3.6.2
collected 1 item / 1 error
====================================================== ERRORS ======================================================
___________________________ ERROR collecting tests/integration/test_web_app/test_main.py ___________________________
ImportError while importing test module '/Users/blarblar/Workspace/talk-booking/services/talk_booking/tests/integration/test_web_app/test_main.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/local/Cellar/python#3.11/3.11.1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
tests/integration/test_web_app/test_main.py:4: in <module>
from web_app.main import app
E ModuleNotFoundError: No module named 'web_app'
============================================= short test summary info ==============================================
ERROR tests/integration/test_web_app/test_main.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
================================================= 1 error in 0.29s =================================================
my directory is
.
├── README.md
└── services
└── talk_booking
├── poetry.lock
├── pyproject.toml
├── tests
│ ├── __init__.py
│ └── integration
│ └── test_web_app
│ ├── __init__.py
│ └── test_main.py
└── web_app
├── __init__.py
└── main.py
i check health-check api work well. 127.0.0.1:8000
when i write test code in main.py, it works well.
if i resolve imporing issue, that will be perfect

Related

Pytest set default path/directory/fodler as project directory (solve FileNotFoundError)

I have the next tree:
root_project/
├── app
│   ├── default_photo_profile.jpg
│   ├── config.py
│   ├── __main__.py # My app are python package, I'm runnig it via "python -m"
│   └── ...
├── tests
│   ├── test_unit.py # import config.py inside
│   ├── functional # import config.py inside
│   ├── pytest.ini
│   └── ...
...
Currently default_photo_profile causing error because tests doesn't have this file.
Reading file in config.py:
DEFAULT_PHOTO_FILE_PATH = Path('default_photo.jpg')
with open(file=DEFAULT_PHOTO_FILE_PATH, mode='rb') as file_obj:
DEFAULT_PHOTO_BYTES = file_obj.read()
How I can solve this?
I tried:
Patch access to default_photo.jpg with fixture - not helped, error during import stage, not executiion.
set flag to pytest comamnd line: --rootdir app - not helped (don't know why).
try/except for reading the file in app.config.py - may help but it's not my intention, I really want raise error if file not found
Put default_photo.jpg inside EVERY test directory - will help bit dirty.
Patch os.path like suggested in https://stackoverflow.com/a/43003192/11277611 - dirty
Include tests into package (move __main__.py into root_project - not sure that it's a good idea (have not enough experience to decide).
Set absolut path to default_photo.jpg - will fail on the production server.
Probably adoptable solutions (What I want):
Set root dir to root_project.app somehow inside pytest.ini to immitate regular execution.
Set root dir to root_project.tests somehow to place file in root of tests and access from any of tests folder.
Try to use following code in config.py:
DEFAULT_PHOTO_FILE_PATH = Path(__file__).parent / 'default_photo.jpg'
with open(file=DEFAULT_PHOTO_FILE_PATH, mode='rb') as file_obj:
DEFAULT_PHOTO_BYTES = file_obj.read()
Is it what you are trying to achieve?

Import File Mismatch in pytest with same test names

This is a much asked question, but none of the solutions mentioned on SO have worked so far.
The folder structure is as follows:
project/
└── tests/
├── conftest.py
├── __init__.py
└── int_tests/
└── test_device.py
└── project_core/
└── tests/
├── conftest.py
├── __init__.py
└── int_tests/
└── test_device.py
import file mismatch:
imported module 'test_device' has this __file__ attribute:
/home/.../project/project_core/tests/int_tests/test_device.py
which is not the same as the test file we want to collect:
/home/.../project/tests/int_tests/test_device.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
Steps tried so far:
Removing pycache and pyc files.
Adding _init to each folder. (As is stated in pytest GIP)
Removing _init from each folder.
Do i need init files in each tests/subfolder?
The same error occurs with conftest.py as well. This error is not limited to vscode-pytest plugin, also occurs on the terminal.
PS : For CI purposes, the system is configured with docker & tox. Development is done in venv.

How to access templates part of a package from a script within a package

I have trouble creating a package with setuptools. I have a repository which I'm cleaning up to make it a package. The directory structure looks something like this
my-proj
├── setup.py
├── MANIFEST.in
├── MakeFile
├── README.rst
├── setup.py
└── myproj
├── __init__.py
├── my_code.py
├── templates
│ ├── template1.yaml
│ ├── template2.yaml
Initial version of "my_code.py" had code snippet which would directly reference the files withing templates folder to do some processing. If I package this using setup tools, I provide the following information in these files:
MANIFEST.in:
include README.rst
include requirements.txt
include LICENSE.txt
recursive-include myproj/templates *
setup.py:
setup(
name='myproj',
package_dir={'testbed_init': 'testbed_init'},
package_data={'templates': ['templates/*'], 'configs': ['configs/*']},
include_package_data=True,
)
My question is as follows. In "my_Code.py" I used to reference templates directly without any problem as I would run script from the myproj folder. If I package this, how can I make sure, I include the templates as part of package and when script runs, I need to open the templates relative to where the package is installed.
Code snippet from my_code.py:
if _type == "a":
temp_file = f"templates/template1.yaml"
else:
temp_file = f"templates/template2.yaml"
build_config(deploy_esx_file, output_file, data)
Code snippet of what happens in build_config:
def build_config(template_file, output_file, inputs):
templateLoader = jinja2.FileSystemLoader(searchpath="./")
templateEnv = jinja2.Environment(loader=templateLoader)
template = templateEnv.get_template(template_file)
outputText = template.render(inputs)
with open(output_file, 'w') as h:
h.write(outputText)

unresolved import for module imports in Visuial Studio Code

I have opened a folder in VS code and I am trying to set it up.
It's a python project and its directory structure is as:
Project
├── common_features
│ ├── ...
├── core
│ ├── features
│ └── main.py
│ └── tests
├── django project
│ ├── django_app1
│ ├── manage.py
│ ├── ...
└── tests
│ ├── ...
└── runner.py
The project runs as a django project from the django_project dir. It uses modules located in common_features and core. Core is designed such that it could also run on its own. You can also run core from runner.py
The problem is that all our module imports are not being resolved but 3rd party packages work well.
unresolved import 'core.config' Python(unresolved-import)
In PyCharm, I have marked Project, core and django_project as "sources root" and it works like a charm. Not sure how to do that in VS code.
I have tried making some changes in launch.json and settings.json but none are working. I am new to VS code so I'm unable to understand what it is that I'm doing wrong.
Thanks.
Can you try adding the following line to your settings.json file?
{
"python.autoComplete.extraPaths": ["./src"]
}
More info about this here: https://github.com/microsoft/python-language-server/blob/master/TROUBLESHOOTING.md#unresolved-import-warnings

Error when attempting to install Karma

I am trying to install Karma using the following command:
C:\Program Files\nodejs>npm install karma
However, I receive the following error when I attempt to install Karma on my Windows 8.1 machine:
npm WARN optional dep failed, continuing fsevents#0.2.0
\
> ws#0.4.31 install C:\Program Files\nodejs\node_modules\karma\node_modules\sock
et.io\node_modules\socket.io-client\node_modules\ws
> (node-gyp rebuild 2> builderror.log) || (exit 0)
|
C:\Program Files\nodejs\node_modules\karma\node_modules\socket.io\node_modules\s
ocket.io-client\node_modules\ws>node "C:\Program Files\nodejs\node_modules\npm\b
in\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild
karma#0.12.17 node_modules\karma
├── di#0.0.1
├── graceful-fs#2.0.3
├── rimraf#2.2.8
├── colors#0.6.2
├── mime#1.2.11
├── q#0.9.7
├── chokidar#0.8.2 (recursive-readdir#0.0.2)
├── minimatch#0.2.14 (sigmund#1.0.0, lru-cache#2.5.0)
├── optimist#0.6.1 (wordwrap#0.0.2, minimist#0.0.10)
├── glob#3.2.11 (inherits#2.0.1, minimatch#0.3.0)
├── source-map#0.1.37 (amdefine#0.1.0)
├── lodash#2.4.1
├── log4js#0.6.15 (semver#1.1.4, async#0.1.15, readable-stream#1.0.27-1)
├── useragent#2.0.9 (lru-cache#2.2.4)
├── http-proxy#0.10.4 (pkginfo#0.3.0, utile#0.2.1)
├── connect#2.12.0 (uid2#0.0.3, methods#0.1.0, cookie-signature#1.0.1, debug#0.8
.1, pause#0.0.1, fresh#0.2.0, qs#0.6.6, bytes#0.2.1, buffer-crc32#0.2.1, raw-bod
y#1.1.2, batch#0.5.0, cookie#0.1.0, negotiator#0.3.0, send#0.1.4, multiparty#2.2
.0)
└── socket.io#0.9.17 (base64id#0.1.0, policyfile#0.0.4, redis#0.7.3, socket.io-c
lient#0.9.16)
I don't think there are any errors.
Just need to install karma-cli
npm install -g karma-cli
Then karma should work.
I had the same problem. In my case the solution to the problem was to use the command in the right folder. I was one folder in hierarchy below where I should have been.