You either need to run your app_test.py from the top, (ie, "python test/app_test.py") or fixup the python path in app_test.py:
import os
import sys
topdir = os.path.join(os.path.dirname(__file__), "..")
sys.path.append(topdir)
The FLASK_APP environment variable is the name of the module to import at flask run. In case that module is incorrectly named you will get an import error upon start (or if debug is enabled when you navigate to the application). It will tell you what it tried to import and why it failed.,To run the application you can either use the flask command or python’s -m switch with Flask. Before you can do that you need to tell your terminal the application to work with by exporting the FLASK_APP environment variable:,The request object is documented in the API section and we will not cover it here in detail (see Request). Here is a broad overview of some of the most common operations. First of all you have to import it from the flask module:,To enable all development features (including debug mode) you can export the FLASK_ENV environment variable and set it to development before running the server:
from flask
import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
$
export FLASK_APP = hello.py
$ flask run
*
Running on http: //127.0.0.1:5000/
C: \path\ to\ app > set FLASK_APP = hello.py
PS C: \path\ to\ app > $env: FLASK_APP = "hello.py"
$
export FLASK_APP = hello.py
$ python - m flask run *
Running on http: //127.0.0.1:5000/
$ flask run--host = 0.0 .0 .0
python.testing.unittestArgs: Looks for any Python (.py) file with "test" in the name in the top-level project folder. All test files must be importable modules or packages. You can customize the file matching pattern with the -p configuration setting, and customize the folder with the -t setting., python.testing.unittestArgs: Looks for any Python (.py) file with "test" in the name in the top-level project folder. All test files must be importable modules or packages. You can customize the file matching pattern with the -p configuration setting, and customize the folder with the -t setting. ,python.testing.pytestArgs: Looks for any Python (.py) file whose name begins with "test_" or ends with "_test", located anywhere within the current folder and all subfolders., python.testing.pytestArgs: Looks for any Python (.py) file whose name begins with "test_" or ends with "_test", located anywhere within the current folder and all subfolders.
For Windows
py - 3 - m pip install pytest - xdist
For macOS/Linux
python3 - m pip install pytest - xdist
Next, create a file named pytest.ini
in your project directory and add the content below, specifying the number of CPUs to be used. For example, to set it up for 4 CPUs:
[pytest] addopts = -n4
Or, if you are using a pyproject.toml
file
[tool.pytest.ini_options]
addopts = "-n 4"
Stop the debugger and correct the faulty code:
# unittest self.assertEqual(inc_dec.decrement(3), 2) # pytest assert inc_dec.decrement(3) == 2
Last Updated : 14 Jul, 2022
Directory Structure
-Folder_1
-
main.py -
Folder_2 -
module1.py
Suppose we have a directory structure like this:
-project
-
Folder_1 -
main.py -
Folder_2 -
subfolder -
new.py
Create the following structure of Python modules:,To use the client fixture we created before, we need to import it. PyCharm will claim that the import is unused, but pytest actually needs it. At the top of the newly created test_root.py file, add an import statement:,Unit testing takes a reductionist approach by focusing on the parts of a program rather than the whole. As a gross generalization, the idea is:,The @pytest.fixture annotation tells pytest that the following function creates (using the yield command) an app for testing. In this case, the function doesn’t do too much, but it could also configure temporary database files or set configurations for testing (e.g. the commented-out app.config line).
$ git clone https: //github.com/Code-the-Change/flask_tests_workshop.git
$ pip3 install virtualenv
PATH="<Path from pip message>:$PATH"
export PATH
$ cd flask_tests_workshop $ virtualenv venv $ source venv / bin / activate $ pip install - r requirements.txt
chmod 700 run.sh
. / run.sh
Within Python modules, pytest also discovers tests using the standard unittest.TestCase subclassing technique.,If you don’t use an editable install and are relying on the fact that Python by default puts the current directory in sys.path to import your package, you can execute python -m pytest to execute the tests against the local copy directly, without using pip.,With --import-mode=importlib things are less convoluted because pytest doesn’t need to change sys.path or sys.modules, making things much less surprising.,The --import-mode=importlib option (see Import modes) does not have any of the drawbacks above because sys.path is not changed when importing test modules, so users that run into this issue are strongly encouraged to try it.
[build - system]
requires = ["setuptools>=42", "wheel"]
build - backend = "setuptools.build_meta"
[metadata] name = PACKAGENAME [options] packages = find:
from setuptools
import setup
setup()
pip install - e.
pyproject.toml setup.cfg mypkg / __init__.py app.py view.py tests / test_app.py test_view.py ...
pyproject.toml setup.cfg mypkg / ... tests / __init__.py foo / __init__.py test_view.py bar / __init__.py test_view.py
Last updated: 2022-04-12
{
"Version": "2012-10-17",
"Statement": [{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "lambda:PublishLayerVersion",
"Resource": "*"
}]
}
$ sudo amazon - linux - extras install python3 .8
$ curl - O https: //bootstrap.pypa.io/get-pip.py
$ python3 .8 get - pip.py--user
$ mkdir python
$ python3 .8 - m pip install pandas - t python /
$ zip - r layer.zip python
$ aws lambda publish - layer - version--layer - name pandas - layer--zip - file fileb: //layer.zip --compatible-runtimes python3.8 --region us-east-1