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

  • 相关阅读:
    gridview展示行号
    DateEdit如果开启Vista模式并显示日期+时间模式
    DevExpress GridView 添加和设置右键菜单
    C# WinForm实现粘贴图片到PictureBox及复制PictureBox中的图片
    dll反编译工具(ILSpy)的使用
    Dev的双击Gridview的DoubleClick
    SQL Server日期时间格式转换字符串详解
    LabelControl文本居中显示
    C# winform 判断click事件点击的是左键还是右键
    Winform窗体状态的判断及调用打开的窗体的方法
  • 原文地址:https://www.cnblogs.com/tuifeideyouran/p/3825313.html
Copyright © 2011-2022 走看看