alexgelman / troubleshooting-chalice-demo

Example repo on how to debug and write integration tests for AWS Chalice applications
5 stars 0 forks source link

not working to me #1

Open Miamoto-Musashi opened 3 years ago

Miamoto-Musashi commented 3 years ago

Hi, seems is not working for me, any hint?

▶ pipenv run pytest
========================================================================================= test session starts ==========================================================================================
platform darwin -- Python 3.8.2, pytest-6.2.0, py-1.10.0, pluggy-0.13.1
rootdir: /Users/enrico/progetti/learn/troubleshooting-chalice-demo
collected 0 items / 1 error

================================================================================================ ERRORS ================================================================================================
______________________________________________________________________________ ERROR collecting tests/test_chalice_it.py _______________________________________________________________________________
ImportError while importing test module '/Users/enrico/progetti/learn/troubleshooting-chalice-demo/tests/test_chalice_it.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_chalice_it.py:3: in <module>
    import app
E   ModuleNotFoundError: No module named 'app'
======================================================================================= short test summary info ========================================================================================
ERROR tests/test_chalice_it.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=========================================================================================== 1 error in 0.11s ===========================================================================================
alexgelman commented 3 years ago

Hi @Miamoto-Musashi Please use python -m pytest to run the tests instead of pipenv run pytest

Alternatively you need to set PYTHONPATH environment variable to include the project directory. You can do so by running export PYTHONPATH=${PYTHONPATH}:${PWD} in the project directory.

mikerayco commented 3 years ago

@Miamoto-Musashi You need to append the path of your app.py

` import json import os from http import HTTPStatus

import pytest from chalice.config import Config from chalice.local import LocalGateway

import sys sys.path.append("app.py PATH HERE") import app

@pytest.fixture def api(): return LocalGateway(app.app,Config())

def test_get_hello_world(api): response = api.handle_request(method='GET', path='/', headers={}, body=None) print(response) assert response['statusCode'] == HTTPStatus.OK response = json.loads(response['body']) assert response['hello'] == 'world' `

We need to do this because in the sample code, the test script is located inside the test folder and we are trying to import app.py which is located in the parent directory.