Django Run a Test By Its Filename - python

I have a django application with a test called test_thing.py
Ive been running my test as such: python3 manage.py test --pattern="test_thing.py"
Is there a way that I can run the test by its filename?
I tried python3 manage.py test apiv2/tests/test_thing.py but I get an error:
TypeError: 'NoneType' object is not iterable
But when I run with the --pattern it works.

Try
python manage.py test apiv2.tests.test_thing.

Related

When I gave command on CMD manage.py runserver it instead took me into VS Code and when i ran it in VS code error was on (from django.core.management)

When I ran the command on CMD manage.py runserver it intern redirected to VS Code. When I tried to run on VS Code it showed the system cannot find the specified path. It was underlining this statement:
try:
from django.core.management
^^^^^^^^^^^^^^^^^^^^^^
first you need to add the python path to your variable environnement
Second
navigate to your django project and use terminal with this command :
python manage.py runserver
use Docs

Django loops infinitly after startapp method call

I'm doing the CS50Web course, and there is a Django project in which I want to create a new app called "wikipedia". The problem is that after I run this command in Windows PowerShell:
python manage.py runserver
this loop happens:
loop print
I don't know what I did wrong for that to happen. After running
python manage.py startapp wikipedia
, I:
went to my Django project and added 'wikipedia' in the installed apps in settings.py
went to urls.py and added a path like this:
path("wiki/", include("wikipedia.urls))
And then I tried running the server. What did I do wrong?
You must have something improperly configured.
Run this:
$ pip uninstall django
$ pip install django
$ django-admin startproject <projectname>
$ cd <projectname>
$ django-admin startapp wikipedia
$ python manage.py makemigrations
$ python manage.py migrate
do what you described in your question
Also, it is always a better idea to run these things in the CMD, rather than in the PowerShell
Are you sure you have url mappings in your wikipedia app urls.py? Not having any patterns there could be the cause of an error.

How to test django app without giving an app_name

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

Django Testserver fixtures not used

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/

See traceback of loaddata command (when error)

I'm trying to load fixture:
python manage.py loaddata stock/fixtures/initial_data.json
But error occurred without traceback (I don't know which model is the problem):
ValueError: Problem installing fixture 'stock/fixtures/initial_data.json': The database backend does not accept 0 as a value for AutoField.
How get the traceback?
There's a --traceback option.
python manage.py loaddata stock/fixtures/initial_data.json --traceback
This isn't mentioned in the loaddata docs, it's part of the default command options.

Categories

Resources