单元测试

from django.test import TestCase
from forms import ProductForm


class SimpleTest(TestCase):
    def test_basic_addition(self):
        """
        Tests that 1 + 1 always equals 2.
        """
        self.assertEqual(1 + 1, 2)


class ProductTest(TestCase):
    def setUp(self):
        self.product = {
            'title':'My Book Title',
            'description':'yyy',
            'image_url':'http://google.com/logo.png',
            'price':1
        }
        f = ProductForm(self.product)
        f.save()
        self.product['title'] = 'My Another Book Title'

    def test_attrs_cannot_empty(self):
        f = ProductForm({})
        self.assertFalse(f.is_valid())
        self.assertTrue(f['title'].errors)
        self.assertTrue(f['description'].errors)
        self.assertTrue(f['price'].errors)
        self.assertTrue(f['image_url'].errors)

    def test_price_positive(self):
        f = ProductForm(self.product)
        self.assertTrue(f.is_valid())

        self.product['price'] = 0
        f = ProductForm(self.product)
        self.assertFalse(f.is_valid())

        self.product['price'] = -1
        f = ProductForm(self.product)
        self.assertFalse(f.is_valid())

        self.product['price'] = 1

    def test_imgae_url_endwiths(self):
        url_base = 'http://google.com/'
        oks = ('fred.gif', 'fred.jpg', 'fred.png', 'FRED.JPG', 'FRED.Jpg')
        bads = ('fred.doc', 'fred.gif/more', 'fred.gif.more')
        for endwith in oks:
            self.product['image_url'] = url_base+endwith
            f = ProductForm(self.product)
            self.assertTrue(f.is_valid(),msg='error when image_url endwith '+endwith)

        for endwith in bads:
            self.product['image_url'] = url_base+endwith
            f = ProductForm(self.product)
            self.assertFalse(f.is_valid(),msg='error when image_url endwith '+endwith)

        self.product['image_url'] = 'http://google.com/logo.png'

命令:

python manage.py test accounts.ProductTest

原文地址:https://www.cnblogs.com/tuifeideyouran/p/3825313.html