# 自动化测试和部署

1、jekins安装 (opens new window)

2、jekins资料 (opens new window)

3、mocha官网 (opens new window)

4、jest官网 (opens new window)

5、chai官网解析 (opens new window)

# 1、单元测试

# 1、单测模式

资料:tdd与bdd的区别(知乎) (opens new window)

行为驱动开发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(()=>{
    // runs 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);
  });
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

# 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);
  });
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

# 4、断言库

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

# 2、自动化测试

资料:结合项目来谈谈 Puppeteer (opens new window)

使用无头浏览器Puppeteer 是 Node.js ⼯具引擎, ⽤来模拟 Chrome 浏览器的运⾏。 Puppeteer 提供了⼀系列API,通过 Chrome DevTools Protocol 协议控制 Chromium/Chrome 浏览器的⾏为。

⾃动化就是借助⼀些⼯具⽐如⽆头浏览器、Jenkins等将原本⼈⼯的⼯作⽤机器按流程去完成。

# 3、持续集成和部署

1、githubhooks (opens new window)

2、jekins自动构建github项目 (opens new window)

3、jekins入门 (opens new window)

  • githubhooks:在Git执⾏特定事件(如commit、push、receive等)后触发运⾏的脚本
  • jekins:持续集成(CI) / 持续交付 (CD)
Last Updated: 11/11/2021, 11:04:53 AM