#Error while setting up tests with Pytest

2 messages · Page 1 of 1 (latest)

dusk cargo
#

Hey! I am running an app in Pycharm on Windows and want to run Pytest.
I keep getting this error while trying to run Pytest.

`rentalApp\conftest.py:21: in <module>
from .models import Rental, Reservation
rentalApp\models.py:8: in <module>
class Rental(models.Model):
........\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\base.py:108: in new
app_config = apps.get_containing_app_config(module)
........\AppData\Local\Programs\Python\Python37\lib\site-packages\django\apps\registry.py:253: in get_containing_app_config
self.check_apps_ready()
........\AppData\Local\Programs\Python\Python37\lib\site-packages\django\apps\registry.py:135: in check_apps_ready
settings.INSTALLED_APPS
........\AppData\Local\Programs\Python\Python37\lib\site-packages\django\conf_init_.py:82: in getattr
self.setup(name)
........\AppData\Local\Programs\Python\Python37\lib\site-packages\django\conf_init
.py:67: in _setup
% (desc, ENVIRONMENT_VARIABLE))
E django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Process finished with exit code 4

Empty suite
`

#

I have the tests.py file inside my "rentalApp".
Here is a small test:

`import pytest

import os
import django

from django.core.management import call_command

from django.urls import reverse

from .models import Rental, Reservation
from .serializers import ReservationSerializer, RentalSerializer

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings")

django.setup()

@pytest.mark.django_db
def test_list_reservations(client):
url = reverse('list-reservation')
response = client.get(url)

reservations = Reservation.objects.all()
expected_data = ReservationSerializer(reservations, many=True).data`

My app is called rentalApp
INSTALLED_APPS = [ 'rest_framework', 'rentalApp.apps.RentalappConfig'

I already tried this: https://stackoverflow.com/questions/26082128/improperlyconfigured-you-must-either-define-the-environment-variable-django-set
And also set theDJANGO_SETTINGS_MODULE=rentalApp.settingsas environment variable on Pycharm but did not work.

Is this a problem of wrongly configured variables in Django, in Pycharm or in Windows?