您当前的位置: 首页 >  单元测试

软件测试凡哥

暂无认证

  • 2浏览

    0关注

    128博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

单元测试很难么?并没有

软件测试凡哥 发布时间:2022-08-11 21:25:07 ,浏览量:2

目录

前言

模型测试

视图测试

模板测试

结语

前言

你可能会用单元测试框架,python的unittest、pytest,Java的Junit、testNG等。

那么你会做单元测试么!当然了,这有什么难的?

test_demo.py

def inc(x):
    return x + 1


def test_answer():
    assert inc(3) == 4

inc() 是定义的一个被测函数,test_anserver() 用于测试上面的一段代码。

通过pytest运行上面的代码:

> pytest test_demo.py
====================== test session starts ======================= platform win32 -- Python 3.7.1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
rootdir: D:\vipcn\demo
plugins: cov-2.7.1, forked-1.0.2, html-1.20.0, metadata-1.8.0, ordering-0.6, parallel-0.0.9, rerunfailures-7.0, xdist-1.28.0, seleniumbase-1.23.10
collected 1 item

test_demo.py .                                              [100%]

==================== 1 passed in 0.08 seconds ====================

单元测试不就是这么单嘛!

那么Web项目中的单元测试如何做?

我们以Django Web框架为例,它是MTV开发模式。接下来会围绕着这个模式介绍如何做测试。

模型测试
  • M 指models,用于定义ORM,即对象关系映射,是通过使用描述对象和数据库之间映射的元数据,将面向对象语言程序中的对象自动持久化到关系数据库中。

models.py 中的代码是这样的:

from django.db import models


class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField(auto_now=True)

这里定义了两个类,这两个类即没有入参,也没有return返回值。如何测试呢?

测试代码如下:

from django.test import TestCase
from myapp.models import Question

class QuestionTestCase(TestCase):

    def setUp(self):
        Question.objects.create(id=1, question_text="你会做单元测试么?")

    def test_question(self):
        """查询id=1的问题"""
        question = Question.objects.get(id=1)
        self.assertEqual(question.question_text, '你会做单元测试么?')

不知道你是否看懂了这段代码,django模型我们可以看作是数据库表,那么对于表的操作就是增删改查,这里先创建一条数据,再查询出这条数据,然后判断其字段是否正确。

视图测试
  • V 指views,用于接收前端发来的请求,可能需要调用数据库,把对应的数据处理之后,和HTML页面一同返回给前端。

views.py 代码如下:

from django.shortcuts import render
from .models import Question


def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    context = {'latest_question_list': latest_question_list}
    return render(request, 'polls/index.html', context)

index() 视图函数确实有入参,request包含的是客户端信息,比如请求的方法,请求的host,请求头Header等,这些客户端数据如何构造? return返回的是HTML页面,以及查询数据库的数据,如何针对这些数据写断言呢?

测试代码如下:

from django.test import TestCase
from myapp.models import Question

class IndexTestCase(TestCase):

    def setUp(self):
        Question.objects.create(id=1, question_text="你会做单元测试么?")

    def test_index(self):
        """测试index视图"""
        response = self.client.get("/index")
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, "polls/index.html")

这里假定当浏览器访问 http://127.0.0.1:8000/index 时调用到index视图,返问题列表页面。

self.client.get() 可以模拟客户端浏览器发送 request GET 请求。拿到服务端的response,判断状态码是否为 200。 self.assertTemplateUsed() 断言返回的页面是否正确。

模板测试
  • T 指Teamplate,主要是HTML页面。用户在浏览器中输入URL地址,最终会得到一个HTML页面。

index.html代码如下:

{% if latest_question_list %}
    
    {% for question in latest_question_list %}
关注
打赏
1664198121
查看更多评论
1.5634s