I am a noob in Travis CI. I created a simple hello world flask app and integrated that with Travis CI. My .travis.yml is like this
os:
- linux
language: python
python:
- "2.6"
- "2.7"
# command to install dependencies
script: python app.py
branches:
only:
- master
Where app.py is the application.The app.py ran successfully while building since it doesnt return anything travis considered it as build fail. What could be the possible fix? I am attaching a screenshot of the build error message.
You are running the dev server unconditionally. Place it in a guard:
if __name__ == '__main__':
app.run()
Change the command to run your tests, rather than the app. Travis is used to build and test, not run your server.
Related
I have a repo that holds two applications, a django one, and a react one.
I'm trying to integrate tests into the pipeline for the django application, currently, the message I'm getting in the pipeline is:
python backend/manage.py test
+ python backend/manage.py $SECRET_KEY
System check identified no issues (0 silenced).
---------------------------------------------------------------
Ran 0 $SECRET_KEYS in 0.000s
However, running the same command in my local docker container finds 11 tests to run. I'm not sure why they aren't being found in the pipeline
My folder structure is like this
backend/
- ...
- app/
-- tests/
- manage.py
frontend/
- ...
bitbucket-pipelines.yml
and my pipelines file:
image: python:3.8
pipelines:
default:
- parallel:
- step:
name: Test
caches:
- pip
script:
- pip install -r requirements.txt
- python backend/manage.py test
The same issue I was facing(django application) in bitbucket pipeline and in local it's working fine when we are doing it in bitbucket pipeline it's not working, it's installed all the requirements and related packages the last command was not running, my assumption is sqlite3 is not support in bitbucket pipeline.
1.Currently, I'm building a flask project and I also wrote some unit testing code. Now I would like to run the Unit test on the GitHub action, but it stuck at the ./run stage(./run will turn on the http://127.0.0.1:5000/), and does not run the $pytest command. I know the reason why $pytest will not be executed because the Github Action is running the port http://127.0.0.1:5000/. In this case, it can not execute any commands after./run. I was wondering can I run $pytest on another terminal in GitHub action? Is this possible?
Here is the output of my github action:
Run cd Flask-backend
cd Flask-backend
./run
pytest
shell: /usr/bin/bash -e {0}
env:
pythonLocation: /opt/hostedtoolcache/Python/3.8.10/x64
* Serving Flask app 'app.py' (lazy loading)
* Environment: development
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 404-425-256
2.Here is my code for yml file:
name: UnitTesting
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Install Python 3
uses: actions/setup-python#v1
with:
python-version: 3.8
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirement.txt
- name: Run tests with pytest
run: |
cd Flask-backend
./run
pytest
You can use nohup command to run the flask server in the background instead of running on a different terminal.
nohup python app.py
Wait for some time after running this command using the sleep command and then run your tests.
I tried it several times but without any success. But there is a simple workaround to test your api locally. I achieved it with a pytest script by using the test_client() from flask package. This client simulates your flask-app.
from api import app
# init test_client
app.config['TESTING'] = True
client = app.test_client()
# to test your app
def test_api():
r = client.get('/your_endpoint')
assert r.status_code == 200
Note that every requests now made directly over the client and the methods which belongs to.
You can find more information here: https://flask.palletsprojects.com/en/2.0.x/testing/
I guess that this is a quite specific situation but I will make the question since I didn't found a relevant answer and might be useful to someone else.
The situation is this:
Windows is the host OS.
There is an Ubuntu VirtualBox (created with vagrant).
My django project folder is shared between windows and ubuntu.
I installed pytest (through pytest-django) both on ubuntu and on
my virtualenv on windows.
If I run the pytest command from the windows virtualenv the tests run
just fine
But in the ubuntu vm terminal I can't just run the pytest command
since I get the error: The program 'pytest' is currently not
installed.
I can run it though with python -m pytest. But in this case I get import file mismatch errors:
========================ERRORS =======================
_____________ ERROR collecting my_django_app/tests.py ____________
import file mismatch:
imported module 'my_django_app.tests' has this **__file__** attribute:
C:\virtualEnvs\my_env\project_src\my_django_app\tests.py
which is not the same as the test file we want to collect:
/vagrant/projects/project_src/my_django_app/tests.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
The message is very clear but I don't know how to overcome this issue.
I use python 2.7.9 and there is no pycache folder (nor ant .pyc compiled files).
I just had to delete the cached compiled python files. These files are inside __pycache__ folders inside the django project folder. Each django app has one pycache folder too.
If you run pytest in windows host, the cached compiled files contain the windows specific paths and try to import modules from there. So the cached files must be deleted. Then you can run pytest in the ubuntu VM using the command: python -m pytest
You can avoid the creation of the compiled files in the first place by running the python interpreter with -B option: python -B pytest
I'm trying to run a Flask application with flask run but no matter what, I receive this error:
Error: Could not locate Flask application. You did not provide the FLASK_APP environment variable.
I'm using virtualenv in my project and I'm running the app on port 80 so I run the command as superuser. Ultimately, I just need to use the flask db init command as described in Flask-Migrate's docs, but flask needs to be able to find the app to do that. Here's what I've tried, with no success:
Exporting the FLASK_APP environment variable, ensuring that it's in my bash profile, then activating virtualenv
$ export FLASK_APP=run.py
$ printenv FLASK_APP
run.py
$ . env/bin/activate
(env) $ sudo flask run
Error: Could not locate Flask application. You did not provide the FLASK_APP environment variable.
Activating virtualenv, then exporting FLASK_APP
$ . env/bin/activate
(env) $ export FLASK_APP=run.py
(env) $ printenv FLASK_APP
run.py
(env) sudo flask run
Error: Could not locate Flask application. You did not provide the FLASK_APP environment variable.
The above two with the full path, /Users/me/code/project/run.py
$ printenv FLASK_APP
/Users/me/code/project/run.py
Project Structure
myproject/
├──app/
| ├── __init__.py
| ├── models.py
| ├── templates/
| └── views.py
├── tests/
├── run.py
├── requirements.txt
└── config.py
So far nothing has worked and the error message is the same in each case. What can I do to fix this error?
If you are on Windows, make sure there is no space around the equal :
set FLASK_APP=app.py
instead of
set FLASK_APP = app.py
That's what happened to me. I got the " You did not provide the FLASK_APP environment variable" error because of the spaces.
Assuming you call app=App(__name__) in your init file. Try this, even though technically it should work with run.py as-well.
export FLASK_APP=app/__init__.py; flask run
Also try doing an echo $FLASK_APP later to see if the value actually gets stored in the environment variable which flask directly accesses and not only the bash profile.
Under Powershell, you have to set the FLASK_APP environment variable as follows:
$env:FLASK_APP = "webapp"
Then you should be able to run python -m flask run inside the hello_app folder. In other words, PowerShell manages environment variables differently, so the standard command-line set FLASK_APP=webapp won't work.
When I drop sudo from sudo flask run, Flask finds $FLASK_APP. However, I get the error message socket.error: [Errno 13] Permission denied. I can't see a way around this, as Flask cannot find $FLASK_APP when I run as superuser. Seems like circular logic.
I've managed to run Flask by changing the port from 80 to 5000 and dropping sudo with flask run. This is fine, I will have to find a way to run the app on port 80 in production though.
I was able to run flask db init after dropping and recreating my database, and removing calls to db.create_all.
Edit - 4/27/17 port 80 was indeed blocked by the firewall on my server (the firewall is beyond my control) so running the app on an open port resolved the issue.
looks you are using bash shell in your terminal. read the flask 2.0 docs. the command should be export FLASK_APP=run and it will have no extension
I faced the same issue. I am using Python 3.10 in VS code.
C:\Users\hansr>flask --version
Python 3.10.1
Flask 2.0.3
Werkzeug 2.0.3
USING set FLASK_APP='main.py' did not work for me and throws the same mentioned error above.
TRY:
$env:FLASK_APP = "main"
python -m flask run
REFERENCE
I am using python3 on Windows and was getting the same error message as the original poster. Here is the code that worked for me:
set FLASK_APP=nameofyourfile.py
I would like to start ipdb every time a test fails.
I tried
$ ipython manage.py test myapp --pdb
but doesn't works.
If you pip install ipdbplugin and pip install django-nose, then add django_nose to your INSTALLED_APPS and set TEST_RUNNER = 'django_nose.NoseTestSuiteRunner', you can then call:
./manage.py test --ipdb
or
./manage.py test --ipdb-failures
See https://github.com/flavioamieiro/nose-ipdb and https://github.com/django-nose/django-nose for further details.
You'll need to install the nose
and django-nose packages. After you configure django-nose for your project, the default test runner and test management command will be beefed up with the nose test runner supports.
Here's a gist with the output of the python manage.py help test command that shows the impressive list of options you get for running tests after the setup.
You can use the django-pdb app. Only install in your settings project