zoukankan      html  css  js  c++  java
  • 单元测试

    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

  • 相关阅读:
    js图片预览插件,不涉及上传
    订单数字提醒的实现
    php对数组中的值进行排序
    sql按照in中的顺序进行排序 mysql
    商城公告功能开发总结
    swiper 多个循环的实现
    thinkphp中的多字段模糊匹配
    phpmailer绑定邮箱
    thinkphp中的session的使用和理解!
    thinkphp引入类的使用
  • 原文地址:https://www.cnblogs.com/tuifeideyouran/p/3825313.html
Copyright © 2011-2022 走看看