18.3. Tests Forms
18.3.1. Example
File myproject/shop/tests/test_forms.py
:
from django.test import SimpleTestCase
from shop.forms import ProductForm
class TestForms(SimpleTestCase):
def test_form_product_valid(self):
form = ProductForm(data={
'name': 'Alpha',
'price': 100.00,
'barcode': '123456789012'})
self.assertTrue(form.is_valid())
def test_form_product_invalid_name_empty(self):
form = ProductForm(data={
'price': 100.00,
'barcode': '123456789012'})
self.assertFalse(form.is_valid())
def test_form_product_invalid_name_invalid(self):
form = ProductForm(data={
'name': None,
'price': 100.00,
'barcode': '123456789012'})
self.assertFalse(form.is_valid())