首页 > 其他分享 >创建第一个Django app-part5

创建第一个Django app-part5

时间:2023-08-25 10:23:31浏览次数:38  
标签:recently app question Django future part5 published test was

自动化测试

开始第一个测试

首先有一个bug

python3 manage.py shell

创建一个测试来暴露这个 bug

将下面的代码写入 polls 应用里的 tests.py 文件内

点击查看代码
from django.test import TestCase

# Create your tests here.
import datetime
from django.utils import timezone
from .models import Question


class QuestionModelTests(TestCase):
    def test_was_published_recently_with_future_question(self):
        """
        was_published_recently() returns False for questions whose pub_date
        is in the future.
        """
        time = timezone.now() + datetime.timedelta(days=30);
        print(time)
        future_question = Question(pub_date = time, question_text="future question")
        '''
        它的返回值应该是False
        '''
        self.assertIs(future_question.was_published_recently(), False) 

运行测试

python3 manage.py test polls

查看输出

点击查看代码
Found 1 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
2023-09-24 01:55:14.798935+00:00
F
======================================================================
FAIL: test_was_published_recently_with_future_question (polls.tests.QuestionModelTests)
was_published_recently() returns False for questions whose pub_date
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/xuteng/Desktop/Study/Python/Django/mysite/polls/tests.py", line 21, in test_was_published_recently_with_future_question
    self.assertIs(future_question.was_published_recently(), False)
AssertionError: True is not False

----------------------------------------------------------------------
Ran 1 test in 0.002s

FAILED (failures=1)
Destroying test database for alias 'default'...

运行过程

  1. python manage.py test polls 将会寻找 polls 应用里的测试代码
  2. 它找到了 django.test.TestCase 的一个子类
  3. 它创建一个特殊的数据库供测试使用
  4. 它在类中寻找测试方法——以 test 开头的方法
  5. 在 test_was_published_recently_with_future_question 方法中,它创建了一个 pub_date 值为 30 天后的 Question 实例
  6. 接着使用 assertls() 方法,发现 was_published_recently() 返回了 True,而我们期望它返回 False
  7. 测试系统通知我们哪些测试样例失败了,和造成测试失败的代码所在的行号

修复这个bug

修改polls.models

点击查看代码
def was_published_recently(self):
        now = timezone.now()
        return now - datetime.timedelta(days=1) <= self.pub_date <= now

测试结果

点击查看代码
Found 1 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
2023-09-24 02:02:22.203513+00:00
.
----------------------------------------------------------------------
Ran 1 test in 0.002s

OK
Destroying test database for alias 'default'...

更加全面的测试

点击查看代码
from django.test import TestCase

# Create your tests here.
import datetime
from django.utils import timezone
from .models import Question


class QuestionModelTests(TestCase):
    def test_was_published_recently_with_future_question(self):
        """
        was_published_recently() returns False for questions whose pub_date
        is in the future.
        """
        time = timezone.now() + datetime.timedelta(days=30);
        print(time)
        future_question = Question(pub_date = time, question_text="future question")
        '''
        它的返回值应该是False
        '''
        self.assertIs(future_question.was_published_recently(), False) 

    def test_was_published_recently_with_old_question(self):
        """
        was_published_recently() returns False for questions whose pub_date
        is older than 1 day.
        """
        time = timezone.now() - datetime.timedelta(days=1, seconds=1)
        old_question = Question(pub_date=time)
        self.assertIs(old_question.was_published_recently(), False)


    def test_was_published_recently_with_recent_question(self):
        """
        was_published_recently() returns True for questions whose pub_date
        is within the last day.
        """
        time = timezone.now() - datetime.timedelta(hours=23, minutes=59, seconds=59)
        recent_question = Question(pub_date=time)
        self.assertIs(recent_question.was_published_recently(), True)

‘’‘
测试结果:

Found 3 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
2023-09-24 02:05:30.209024+00:00
...
----------------------------------------------------------------------
Ran 3 tests in 0.004s

OK
Destroying test database for alias 'default'...

’‘’

标签:recently,app,question,Django,future,part5,published,test,was
From: https://www.cnblogs.com/hyattxt/p/17656172.html

相关文章

  • uniapp路由跳转后通过uni.$emit和uni.$on页面通讯后,接收到数据但是却赋值不了
    原因:跳转到未渲染过的页面时,页面还没渲染就进行赋值操作,所以赋值不上去。      如果往后跳转,可以使用uni.$emit和uni.$on页面传值。即uni.navigateBack({delta:1}),尽量不要使用uni.navigateTo(会造成页面重叠)。能够接收到数据并赋值。解决办法:使用EventChannel代码......
  • Spring源码搭建导依赖时报错:Failed to apply plugin 'kotlin'.
    原因是kotlin插件的版本与gradle中指定的版本不一致,我的是1.8.0,spring5.3.x版本gradle配置文件指定的kotlin版本是1.5.32,修改成1.8.0......
  • python独立脚本应用Django项目的环境
    一、需求说明一直用Django在开发一个网站项目,其中的注册用户和登录,都是使用Django自带的认证系统。主要是对密码的加密,在注册或者登录的时候,前端传递多来的密码,我会使用Django的set_password()方法在加密一次经过加密后的数据库中的数据样子如下:......
  • 0x00 BabyDjango,启动
    0x00BabyDjango,启动新建项目此处我使用之前的解释器(主要是不想再从0到×再安装一些包,难受...)原先解释器中得先装好Django第三方库新建项目初始目录如下启动在终端中,指定地址和端口进行运行pythonmanage.pyrunserverip:portDjango项目结构说明一个常规目录......
  • springboot3 集成mybatis 和通用mapper
    xml版本查看:https://www.cnblogs.com/binz/p/6564490.htmlspringboot3.x以前的版本查看https://www.cnblogs.com/binz/p/17421063.htmlspringboot3.x查看  https://www.cnblogs.com/binz/p/17654403.html1、pom引用<parent><groupId>org.springframework.boot</gro......
  • app和web测试的区分
    web测试和app的测试区别有哪些????一、系统架构不同web项目主要是基于浏览器的bs架构,而app项目主要是基于手机端的cs架构二、测试方法不同功能测试:web不支持离线浏览,但是有的app支持;性能测试:web主要关注服务器性能,app除了服务器还有考虑手机端的性能;兼容测试:web主要考虑浏览器的兼容性......
  • app商城测试点
    原文:https://blog.csdn.net/qq_40807544/article/details/128287561购物车功能测试:1.页面是否与UI保持一致2.能否正常加入购物车3.账号未登录能否添加商品到购物车4.账号登录能否添加商品到购物车5.没有库存的商品是否可以加入购物车6.单个商品的数量上限最多能添加到购物车7.收......
  • 配置application.yml踩的坑
    spring:application:name:user-centerdatasource:driver-class-name:com.mysql.cj.jdbc.Driverurl:jdbc:mysql://localhost:3306/schedulingusername:rootpassword:123456server:port:8080mybatis-plus:configurati......
  • Python Web:Django、Flask和FastAPI框架对比
    Django、Flask和FastAPI是PythonWeb框架中的三个主要代表。这些框架都有着各自的优点和缺点,适合不同类型和规模的应用程序。Django:Django是一个全功能的Web框架,它提供了很多内置的应用程序和工具,使得开发Web应用程序更加容易。Django采用了MTV(模型-模板-视图)设计模式,提供ORM......
  • 直播app源码,会话描述协议SDP:高质量平台服务
    摘要:SDP协议又称为会话描述协议,在直播app源码平台中,通过定义实时通信参数,管理会话信息和媒体数据,来为用户提供实时通信服务,确保通信的质量与稳定,例如:在直播app源码平台的直播间中,SDP协议可以为观众与主播实时通信,来实现主播与观众的实时交流。  引言:在这个现代大部分人都......