自动化测试和部署

1、jekins安装(opens new window)
2、jekins资料(opens new window)
3、mocha官网(opens new window)
4、jest官网(opens new window)
5、chai官网解析

1、单元测试

1、单元模式

资料:tdd与bdd的区别(知乎)
行动驱动开发BDD、测试驱动开发TDD

2、Mocha

Mocha单元测试库,支持BDD/TDD多种测试风格,默认使用BDD库,无内置断言库。

  • describe:行为描述,代表一个测试块要干什么,是一组测试单元的集合;
  • it:描述了一个测试单元,是最小的测试单位;
  • before:Hook 函数,在执行该测试块之前执行;
  • after:Hook 函数,在执行测试块之后执行;
  • beforeEach:Hook 函数,在执行该测试块中每个测试单元之前执行;
  • afterEach:Hook 函数,在执行该测试块中每个测试单元之后执行。

注意:测试异步执行接口的返回。只需要在用例函数里边加一个done回调,异步代码执行完毕后调用以下done,就可以通知mocha,我执行完啦,去执行下一个用例函数。

// example.spec.js
import { shallowMount } from "@vue/test-utils";
import HelloWorld from "@/components/HelloWorld.vue";

describe("HelloWorld.vue", () => {
  before(() => {
    // run before all tests in this block
  });
  it("renders props.msg when passed", () => {
    const msg = "new message";
    const wrapper = shallowMount(HelloWorld, {
      propsData: { msg }
    });
    expect(wrapper.text()).toMatch(msg);
  });
});

// 异步:回调使用done()返回
describe("#save()", function() {
  it("should save without error", function(done) {
    var user = new User("Luna");
    user.save(function(err) {
      if (err) done(err);
      else done();
    });
  });
});

// 异步promise:直接使用
describe("#find()", function() {
  it("respond with matching records", function() {
    return db.find({ type: "User" }).should.eventually.have.length(3);
  });
});

// async/await
describe("#find()", function() {
  it("responds with matching records", async function() {
    const users = await db.find({ type: "User" });
    users.should.have.length(3);
  });
});

3、Jest

  • 方法:test
  • 断言:expect
  • 钩子:beforeAll、afterAll、beforeEach、afterEach
  • 模块化:describe
  • mock:jest.fn()
test("two plus two is four", () => {
  expect(2 + 2).toBe(4); // .not.toBe
  const data = { one: 1 };
  data["two"] = 2;
  expect(data).toEqual({ one: 1, two: 2 });
});

test("object assignment", () => {
  expect(n).toBeNull();
  expect(n).toBeDefined();
  expect(n).not.toBeUndefined();
  expect(n).not.toBeTruthy();
  expect(n).toBeFalsy();
});

// 异步回调:使用done函数
test("the data is peanut butter", done => {
  function callback(data) {
    try {
      expect(data).toBe("peanut butter");
      done();
    } catch (error) {
      done(error);
    }
  }
  fetchData(callback);
});

// promise
test("the data is peanut butter", () => {
  return fetchData().then(data => {
    expect(data).toBe("peanut butter");
  });
});
test("the data is peanut butter", () => {
  return expect(fetchData()).resolves.toBe("peanut butter");
});
test("the fetch fails with an error", () => {
  return expect(fetchData()).rejects.toMatch("error");
});

// async和await
test("the fetch fails with an error", async () => {
  expect.assertions(1);
  try {
    await fetchData();
  } catch (e) {
    expect(e).toMatch("error");
  }
});

// scoping
describe("matching cities to foods", () => {
  // Applies only to tests in this describe block
  beforeEach(() => {
    return initializeFoodDatabase();
  });

  test("Vienna <3 veal", () => {
    expect(isValidCityFoodPair("Vienna", "Wiener Schnitzel")).toBe(true);
  });

  test("San Juan <3 plantains", () => {
    expect(isValidCityFoodPair("San Juan", "Mofongo")).toBe(true);
  });
});

4、断言库

  • nodejs的assert模块
  • chai:assert、expect、should

2、自动化测试

资料:结合项目来谈谈 Puppeteer
使用无头浏览器Puppeteer 是 Node.js 工具引擎,用来模拟 Chrome 浏览器的运行。 Puppeteer 提供了一系列API,通过 Chrome DevTools Protocal 协议控制 Chrmium/Chrome 浏览器的行为。
自动化就是借助一些工具比如无头浏览器、Jenkins等将原本人工的工作用机器按流程去完成。

3、持续集成和部署

1、githubhooks(opens new window)
2、jekins自动构建github项目(opens new window)
3、jekins入门

  • githubhooks:在Git执行特定事件(如commit、push、receive等)后触发运行的脚本
  • jekins:持续继承(CI) / 持续交付(CD)
posted @ 2023-02-27 11:21  中亿丰数字科技  阅读(59)  评论(0)    收藏  举报