My project structure looks like this: backend/src/apps/app_name/tests/foo.py and when I'm trying to run the tests by entering a command python manage.py test it runs 0 tests. The only correct way to test is python manage.py test src.apps.app_name but I'm to lazy to do it with every app. 😄
Change
backend/src/apps/app_name/tests/foo.py
to
backend/src/apps/app_name/tests/test_foo.py
Make sure all your test files and functions start with test
Related
I have searched for this all over the internet and couldn't find an answer.
The output of the job is something like this:
test/test_something.py:25: AssertionError
========================= 1 failed, 64 passed in 2.10s =========================
Job succeeded
my .gitlab-ci.yml file for the test:
run_tests:
stage: test
tags:
- tests
script:
- echo "Running tests"
- ./venv/bin/python -m pytest
I'm using shell executor.
anyone faced this problem before? as I understand that gitlab CI depends on the exit code of the pytest and it should fail if the exit code is not zero, but in this case pytest should have exit code 1 since a test failed.
It's not something about your gitlab-ci script but rather your pytest script (the script or module you are using to run your tests).
Following I included an example for you, assuming that you might use something like Flask CLI to manage your tests.
You can use SystemExit to raise the exit code. If anything other than 0 is returned, it will fail the process. In a nutshell, GitLab stages are going to succeed if the exit code that is returned is 0.
Pytest only runs the tests but doesn't return the exit code. You can implement this into your code:
your manage.py (assuming you are using flask CLI) will look like something as follows:
import pytest
import click
from flask import Flask
app = Flask(__name__)
#app.cli.command("tests")
#click.argument("option", required=False)
def run_test_with_option(option: str = None):
if option is None:
raise SystemExit(pytest.main())
Note how the above code is raiseing and defining a flask CLI command of tests. To run your code you can simply add the following to your gitlab-ci script:
run_tests:
stage: test
tags:
- tests
variables:
FLASK_APP: manage.py
script:
- echo "Running tests"
- ./venv/bin/activate
- flask tests
The script that will run your test will be flask tests which is raising SystemExit as shown.
FYI: you may not use Flask CLI to manage your tests script, and simply want to run a test script. In that case, this answer might also help.
I want to run the test cases for my python code where am using flask framework.
You can use this command to run the test suite in flask framework
pytest --cov=src --cov-report=html
That depends on how you've written the test cases in the first place. Happily, though, Pytest tends to be able to run whatever at least close to standard tests you have, and pytest-cov adds coverage.
So, once you have pytest and pytest-cov installed, you can
pytest --cov . --cov-report term --cov-report html
and you'll get
a report of coverage in the console
a htmlcov/ directory with pretty, colorful coverage information.
If you've written your code as REST API. I would recommend pyresttest.
You can write your test cases as simple as this in a test.yaml file.
- test: # create entity by POST
- name: "Create person"
- url: "/api/person/"
- method: "POST"
- body: '{"first_name": "Ahmet","last_name": "Tatanga"}'
- headers: {Content-Type: application/json}
Then you just run this test case by
pyresttest test.yaml
You can implement some validators for returned JSON. To learn more please check here.
I still pretty new to the use of crontab in Django. I have followed the instruction in this link https://hprog99.wordpress.com/2014/08/14/how-to-setup-django-cron-jobs/ to help me set up my method, my_scheduled_job() in cron.py file, which I want to call every five minutes.
Here is my updated setting.py
INSTALLED_APPS = (
'django_crontab',
...)
CRONJOBS = [
('*/5 * * * *', 'myproject.cron.my_scheduled_job')
]
After which I ran this: python manage.py crontab add
Output: adding cronjob: (d0428c9ae8227ed78b17bf32aca4bc67) -> ('*/5 * * * *', 'cinemas.cron.my_scheduled_job')
Next: Nothing happens.
How do I start this cron job locally? Is there a way to test if my job ran normally?
In django you can setup cron using django-chronograph or chronograph.
Django-chronograph is a simple application that allows you to control the frequency at which a Django management command gets run.
Step 1:
Create management command of your task in django. For creating django management command refer Writing custom django-admin commands.
Step 2:
After creating django management command configure command in cronograph.
Hope this helps you.
first, you must Specify your project profile, then add the cron job, if your project name is foo, then it is like:
export FOO_PROFILE = 'local'
python manage.py crontab add
and , before run above command, in your settings.local you should config cron environment first, something like:
# django-crontab config
CRONTAB_DJANGO_SETTINGS_MODULE = 'gold.settings.local'
Is there a test tab in Python? Probably a simulation somewhere to get tutorial. Try key word search in agent answer or similar help and command program.
python manage.py runserver
Once you start the server, crontab will automatically execute based on the time specified in settings.py.
I wanted to start a testserver in Django so that I am able to test my API via Jasmine/FrisbyJs.
For that I found that python3 manage.py testserver would create a test-db and load all the testdata provided in the fixtures, which sound exactly what I need. I am NOT running Django-Testcases ATM.
I created a Fixture called testdata.json and stored it within ./fixtures.
I also set up my ./projname/settings.py like that:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Fixture Dir
FIXTURE_DIRS = (
os.path.join(BASE_DIR, 'fixtures'),
)
I also made sure python3 manage.py loaddata testdata works, which it does:
Installed 1 object(s) from 1 fixture(s)
However, running python3 manage.py testserver testdata.json or python3 manage.py testserver testdata leads to this error:
CommandError: Error: No database fixture specified. Please provide the path of at least one fixture in the command line.
What can I do about this?
python manage.py testserver [Fixture Name]
eg :
python manage.py testserver SampleData
SampleData.json file will be in app/fixture/
How would I do the following in my wsgi file, which is imported by apache on production?
$ source env_template.sh
$ python manage.py runserver
>>> Stage: debugging
Validating models...
In my settings file I have stuff like the following, which source sets:
AWS_ACCESS_KEY = os.environ['AWS_ACCESS_KEY']
I would like apache to load all the variables in env_template.sh on starting it. Am I able to do that in the wsgi.py file?
Not in any sane manner. I recommend that you either restrict the file to simple "key=value" lines and then parse it out, or move the specific values to separate files and then read them out in both scripts.