我正在学习测试。
我想测试一下我拥有的SignupForm,为此,我正在使用软件包coverage
。
但是,在运行时:
coverage run manage.py test shop/tests -v
我得到:
$ coverage run manage.py test shop/tests -v
usage: manage.py test [-h] [--noinput] [--failfast] [--testrunner TESTRUNNER]
[-t TOP_LEVEL] [-p PATTERN] [-k] [-r] [--debug-mode]
[-d] [--parallel [N]] [--tag TAGS]
[--exclude-tag EXCLUDE_TAGS] [--version] [-v {0,1,2,3}]
[--settings SETTINGS] [--pythonpath PYTHONPATH]
[--traceback] [--no-color] [--force-color]
[test_label [test_label ...]]
manage.py test: error: argument -v/--verbosity: expected one argument
(stickers-gallito-app)
shop / tests / test_forms.py:
from django.test import TestCase
from django.utils import timezone
from django.urls import reverse
from shop.forms import SignUpForm
#coverage run manage.py test shop/tests -v
class SignUpFormTest(TestCase):
def test_signup_form(self):
form_data = {'first_name': 'oma',
'last_name': 'gonza',
'username': 'omagonza',
'email': 'oma.gonzales@gmail.com',
'password1': 'caballo123',
'password2': 'caballo123'}
form = SignUpForm(data=form_data)
self.assertTrue(form.is_valid())
shop / forms.py:
class SignUpForm(UserCreationForm):
error_messages = {
'password_mismatch': "Las contraseñas no coinciden.",
}
first_name = forms.CharField(label="Nombre", max_length=100, required=True)
last_name = forms.CharField(label='Apellido', max_length=100, required=True)
username = forms.CharField(label='Nombre de usuario', max_length=100, required=True,
error_messages={'invalid': "you custom error message"})
email = forms.EmailField(label='Correo electrónico', max_length=60, required=True)
password1 = forms.CharField(label='Contraseña', widget=forms.PasswordInput)
password2 = forms.CharField(label='Confirmar contraseña', widget=forms.PasswordInput)
def __init__(self, *args, **kwargs):
super(SignUpForm, self).__init__(*args, **kwargs)
for fieldname in ['username', 'password1', 'password2']:
self.fields[fieldname].help_text = None
def clean_password2(self):
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise forms.ValidationError(
self.error_messages['password_mismatch'],
code='password_mismatch',
)
return password2
class Meta:
model = User
fields = ('first_name', 'last_name', 'username', 'email', 'password1',
'password2')
结构:
|_shop
|_migrations
|_templates
|_tests
|___init__.py #empty file
|_test_forms.py