• 单元测试


    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

  • 相关阅读:
    windows下mysql配置,my.ini配置文件
    android studio 3.1.4下载安装配置(附旧版本下载地址)
    tomcat在eclipse创建过程分析
    Jfinal本地eclipse+tomcat运行项目时候遇到错误Exception starting filter
    国内maven仓库地址资源汇总
    Android开发 BottomNavigationView学习
    Android开发 MediaPlayer播放本地视频完善的demo(只是代码记录)
    视频测试网址
    Android开发 处理拍照完成后的照片角度
    图像绘制工具网址
  • 原文地址:https://www.cnblogs.com/tuifeideyouran/p/3825313.html
Copyright © 2020-2023  润新知