使用mocha完成测试用例自动化脚本,本地生产环境皆可用(nodejs)

2021/10/28 11:09:51

本文主要是介绍使用mocha完成测试用例自动化脚本,本地生产环境皆可用(nodejs),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

'use strict';
const assert = require('assert');
const axios = require('axios');
const address = 'http://192.168.3.19:7001/' //生产就是生产的地址
// mocha ./test/app/getscript.test.js
describe('测试书籍相关请求', () => {
  let bookId
  let token
  it('书籍列表', async () => {
    const book = await axios.get(`${address}api/book`, { json: true })
    assert.strictEqual(book.data.code, 'success', '书籍列表 返回码错误')
    bookId = book.data.data.rows[1].id
  })
  it('加入书架', async () => {
    let user = await axios.post(`${address}kyy/user/login`, {
      username: '15929322774',
      password: '123456',
      appname: 'com.kyy.lwyd',
    })
    assert.equal(user.status, 200, '用户登录 返回码错误')
    token = user.data.data
    let rack = await axios({
      method: 'post',
      url: `${address}api/book/rack`,
      headers: { 'x-token': token },
      data: {
        id: bookId,
      },
    })
    assert.equal(rack.data.code, 'success', '加入书架 返回码错误')
  })
  it('查看书架', async () => {
    let getrack = await axios({
      method: 'get',
      url: `${address}api/book/rack`,
      headers: { 'x-token': token },
    })
    assert.equal(getrack.data.code, 'success', '查看书架 返回码错误')
  })
  it('浏览记录', async () => {
    let scan = await axios({
      method: 'get',
      url: `${address}api/book/scan`,
      headers: { 'x-token': token },
    })
    assert.equal(scan.data.code, 'success', '浏览记录 返回码错误')
    // let { body: { code, data } } = await app.httpRequest().get('/api/book/scan').set('x-token', token).send().expect(200);
    // assert.equal(code, 'success', '浏览记录 返回码错误')
    // scanrecond = data.rows;
  })
  it('书籍信息', async () => {
    const bookinfo = await axios.get(`${address}api/book/${bookId}`, { json: true })
    assert.strictEqual(bookinfo.data.code, 'success', '书籍信息列表 返回码错误')
  })
  let section
  it('书籍章节', async () => {
    const booksection = await axios.get(`${address}api/book/section`, {
      params: {
        id: bookId,
      } })
    assert.strictEqual(booksection.data.code, 'success', '书籍章节列表 返回码错误')
    section = booksection.data.data.rows[1]
  })
  it('章节跳转', async () => {
    const bookjump = await axios.get(`${address}api/book/section/jump`, {
      params: {
        bookId,
        sort: section.sort + 1,
      } })
    assert.strictEqual(bookjump.data.code, 'success', '书籍章节跳转 返回码错误')
  });
  it('章节详情', async () => {
    const booksectioninfo = await axios.get(`${address}api/book/section/${section.id}`, { json: true })
    assert.strictEqual(booksectioninfo.data.code, 'success', '章节详情 返回码错误')
  })
  it('男生热门更新', async () => {
    const boyhot = await axios.get(`${address}api/v2/book/config`, { json: true, params: {
      type: 'hotUpdate', size: 5,
    } })
    assert.strictEqual(boyhot.data.code, 'success', '男生热门更新 返回码错误')
  })
  it('女生热门更新', async () => {
    const girlhot = await axios.get(`${address}api/v2/book/config`, { json: true, params: {
      type: 'nvhotUpdate', size: 5,
    } })
    assert.strictEqual(girlhot.data.code, 'success', '女生热门更新 返回码错误')
  })
  it('男生重磅推荐', async () => {
    const recom = await axios.get(`${address}api/v2/book/config`, { json: true, params: {
      type: 'recom', size: 5,
    } })
    assert.strictEqual(recom.data.code, 'success', '男生重磅推荐 返回码错误')
  })
  it('女生重磅推荐', async () => {
    const nvrecom = await axios.get(`${address}api/v2/book/config`, { json: true, params: {
      type: 'nvrecom', size: 5,
    } })
    assert.strictEqual(nvrecom.data.code, 'success', '女生重磅推荐 返回码错误')
  })
  it('男生书架推荐', async () => {
    const rack = await axios.get(`${address}api/v2/book/config`, { json: true, params: {
      type: 'rack', size: 5,
    } })
    assert.strictEqual(rack.data.code, 'success', '男生书架推荐 返回码错误')
  })
  it('女生书架推荐', async () => {
    const nvrack = await axios.get(`${address}api/v2/book/config`, { json: true, params: {
      type: 'nvrack', size: 5,
    } })
    assert.strictEqual(nvrack.data.code, 'success', '女生书架推荐 返回码错误')
  })
})
describe('测试文章相关请求', () => {
  let token
  it('cms分类列表 ', async () => {
    const category = await axios.get(`${address}api/cms/category/list`, { json: true })
    assert.strictEqual(category.data.code, 'success', '分类列表 返回码错误')
  })
  it('cms 标签列表', async () => {
    const tag = await axios.get(`${address}api/cms/tag/list`, { json: true })
    assert.strictEqual(tag.data.code, 'success', '标签列表 返回码错误')
  })
  let contentId
  it('cms 文章列表', async () => {
    const content = await axios.get(`${address}api/cms/content`, { json: true, params: {
      page: 1, size: 20, contentType: 1,
    } })
    for (let con of content.data.data.rows) {
      if (con.content === '') {
        throw new Error(`文章存在空,文章ID为${con.id}`)
      }
    }
    assert.strictEqual(content.data.code, 'success', '文章列表 返回码错误')
    contentId = content.data.data.rows[1].id
  })
  it('cms 点赞收藏文章', async () => {
    let user = await axios.post(`${address}kyy/user/login`, {
      username: '15929322774',
      password: '123456',
      appname: 'com.kyy.lwyd',
    })
    assert.equal(user.status, 200, '用户登录 返回码错误')
    token = user.data.data
    let collection = await axios({
      method: 'put',
      url: `${address}api/cms/content/collection`,
      headers: { 'x-token': token },
      data: { contentId, type: 'collection', status: 1 },
    })
    assert.equal(collection.data.code, 'success', '点赞收藏文章 返回码错误')
  })
  it('cms 收藏列表', async () => {
    let collectionlist = await axios({
      method: 'get',
      url: `${address}api/cms/content/collection/list`,
      headers: { 'x-token': token },
    })
    assert.equal(collectionlist.data.code, 'success', '收藏列表 返回码错误')
  })
  it('cms 关注粉丝列表', async () => {
    let attention = await axios({
      method: 'get',
      url: `${address}api/cms/attention/list`,
      headers: { 'x-token': token },
      params: { type: 'focus' },
    })
    assert.equal(attention.data.code, 'success', '关注粉丝列表 返回码错误')
  })
  it('cms 关注用户文章列表', async () => {
    let attention = await axios({
      method: 'get',
      url: `${address}api/cms/focus`,
      headers: { 'x-token': token },
    })
    assert.equal(attention.data.code, 'success', '关注用户文章列表 返回码错误')
  })
  it('cms 文章列表详情', async () => {
    const contentinfo = await axios.get(`${address}api/cms/content/${contentId}`, { json: true })
    assert.strictEqual(contentinfo.data.code, 'success', '标签列表 返回码错误')
  })
  it('cms 评论列表', async () => {
    const comment = await axios.get(`${address}api/cms/comment`, { json: true, params: {
      page: 1, size: 20, contentId, pid: 0,
    } })
    assert.strictEqual(comment.data.code, 'success', '文章列表 返回码错误')
  })
})
describe('测试用户协议/隐私政策', () => {
  it('用户协议/隐私政策', async () => {
    const agreement = await axios.get(`${address}api/kyy/agreement`, { json: true, params: {
      companyname: '西安星喵无限',
    } })
    assert.strictEqual(agreement.status, 200, '用户协议/隐私政策 返回码错误')
  })
})
describe('客户端setting数据', () => {
  it('客户端setting数据', async () => {
    const agreement = await axios.get(`${address}api/app/setting`, { json: true, params: {
      appname: 'com.kyy.lwyd',
    } })
    assert.strictEqual(agreement.status, 200, '用户协议/隐私政策 返回码错误')
  })
})
describe('用户相关的测试脚本', () => {
  let token
  it('用户登录', async () => {
    let user = await axios.post(`${address}kyy/user/login`, {
      username: '15929322774',
      password: '123456',
      appname: 'com.kyy.lwyd',
    })
    assert.equal(user.status, 200, '用户登录 返回码错误')
    token = user.data.data
  })
  let uid
  it('用户详情', async () => {
    let detail = await axios({
      method: 'get',
      url: `${address}kyy/user/detail`,
      headers: { 'x-token': token },
    })
    assert.equal(detail.data.code, 'success', '用户详情 返回码错误')
    uid = detail.data.data.id
  })
  it('用户抽奖.', async () => {
    let lottery = await axios({
      method: 'get',
      url: `${address}kyy/user/lottery`,
      params: { game: 'dazhuanpan' },
      headers: { 'x-token': token },
    })
    assert.equal(lottery.data.code, 'success', '用户抽奖 返回码错误')
  })
  it('用户签到', async () => {
    let dailysignin = await axios({
      method: 'get',
      url: `${address}kyy/user/dailysignin`,
      headers: { 'x-token': token },
      params: { uid },
    })
    assert.equal(dailysignin.data.code, 'success', '用户签到 返回码错误')
    it('用户添加桌面', async () => {
      let adddesktop = await axios({
        method: 'get',
        url: `${address}api/kyy/add_desktop`,
        headers: { 'x-token': token },
      })
      assert.equal(adddesktop.data.code, 'success', '用户添加桌面 返回码错误')
    })
    it('用户举报,意见反馈', async () => {
      let feedback = await axios({
        method: 'post',
        url: `${address}api/kyy/feedback`,
        headers: { 'x-token': token },
        data: {
          source: '意见反馈',
          reason: 'seqing',
          details: '我就是不喜欢',
          phone: 1203158370,
        },
      })
      assert.equal(feedback.data.code, 'success', '用户举报,意见反馈 返回码错误')
    })
    it('做任务,领金币状态获取', async () => {
      let reward = await axios({
        method: 'get',
        url: `${address}api/kyy/user/task_reward`,
        headers: { 'x-token': token },
        params: { task: 1 },
      })
      assert.equal(reward.data.code, 'success', '做任务,领金币状态获取 返回码错误')
    })
    it(' 达到要求创建金币未领取记录(阅读30分钟)', async () => {
      let transaction = await axios({
        method: 'post',
        url: `${address}api/kyy/user/crate_transaction`,
        headers: { 'x-token': token },
      })
      assert.equal(transaction.data.code, 'success', '达到要求创建金币未领取记录(阅读30分钟) 返回码错误')
    })
    it(' 金币消费记录、金币收入记录', async () => {
      let record = await axios({
        method: 'get',
        url: `${address}kyy/user/gold/record`,
        headers: { 'x-token': token },
        params: { page: 1,
          limit: 10,
          trading_type: 'reward' },
      })
      assert.equal(record.data.code, 'success', '金币消费记录、金币收入记录 返回码错误')
    })
  })
})



这篇关于使用mocha完成测试用例自动化脚本,本地生产环境皆可用(nodejs)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程