Я использую python 3.5 и Django 3.0.5 и пытаюсь протестировать мое приложение в tests.py
, но появилась ошибка.
Ошибка
cls.subcategoria = SubCategoria.objects.create(id=7, descripcion='Desarrollo Web test', categoria_id=categoria)
NameError: name 'categoria' is not defined
Вот мой код:
from django.db import models
class Categoria(models.Model):
descripcion = models.CharField(
max_length=100,
help_text='Descripción de la Categoría',
unique=True
)
def __str__(self):
return '{}'.format(self.descripcion)
class Meta:
verbose_name_plural = "Categorías"
class SubCategoria(models.Model):
categoria = models.ForeignKey(Categoria, on_delete=models.CASCADE)
descripcion = models.CharField(
max_length=100,
help_text='Descripción de la Sub Categoría'
)
def __str__(self):
return '{}:{}'.format(self.categoria.descripcion,self.descripcion)
class Meta:
verbose_name_plural = "Sub Categorías"
unique_together = ('categoria','descripcion')
class Producto(models.Model):
subcategoria = models.ForeignKey(SubCategoria, on_delete=models.CASCADE)
descripcion = models.CharField(
max_length=100,
help_text='Descripción del Producto',
unique=True
)
fecha_creado = models.DateTimeField()
vendido = models.BooleanField(default=False)
def __str__(self):
return self.descripcion
class Meta:
verbose_name_plural = "Productos"
from django.contrib.auth.models import User, AnonymousUser
from django.test import RequestFactory
from django.urls import reverse, resolve
from mixer.backend.django import mixer
from app.apiviews import ProductoDetalle
from app.models import Producto, SubCategoria, Categoria
from django.test import TestCase
import pytest
import datetime
class TestUrls(TestCase):
def test_detail_url(self):
path = reverse('producto_detalle', kwargs={'pk':1})
assert resolve(path).view_name == 'producto_detalle'
class TestModelsCase(TestCase):
@classmethod
def setUpTestData(cls):
#Set up non-modified objects used by all test methods
cls.categoria = Categoria.objects.create(id=7, descripcion='Test Django')
cls.subcategoria = SubCategoria.objects.create(id=7, descripcion='Desarrollo Web test', categoria_id=categoria)
cls.prodc = Producto.objects.create(id=7, descripcion='django 3.0', fecha_creado=datetime.now.today(), vendido=False, subcategoria_id=subcategoria)
def test_alice_first_name(self):
self.assertEqual(self.categoria.id,7)
Сообщение об ошибке
(venv) λ python manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
E.
======================================================================
ERROR: setUpClass (core.test.test_models.TestModelsCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Workspace\workspace_python\Project_Products\venv\lib\site-packages\django\test\testcases.py", line 1137, in setUpClass
cls.setUpTestData()
File "C:\Workspace\workspace_python\Project_Products\core\test\test_models.py", line 24, in setUpTestData
cls.subcategoria = SubCategoria.objects.create(id=7, descripcion='Desarrollo Web test', categoria_id=categoria)
NameError: name 'categoria' is not defined
----------------------------------------------------------------------
Ran 1 test in 0.018s
enter code here
FAILED (errors=1)
Destroying test database for alias 'default'...