跳到主要内容

Test

testXBell 中最基础的工具函数,用于声明和编写测试用例。

test

函数签名: test(displayName, testFunction)

  • displayName <string> 用例描述
  • testFunction <(args: TestArguments) => void | Promise<void>> 测试函数,参数中含有测试所需的工具方法
import test from 'xbell';

test('goto github', async ({ page }) => {
await page.goto('https://github.com');
});

test.extend

函数签名: test.extend(extendFunction)

  • extendFunction <(testArgs: TestArguments) => any>

"test.extend" 可以扩展或自定义用例的环境,它将在每个用例开始之前执行

import { test as baisc } from 'xbell';

const test = basic.extend(({ expect }) => {
const data = [ 1, 2, 3, ];
return {
expect,
data,
};
});

test('extend demo', ({ expect, data }) => {
expect(data[0]).toBe(1);
});

test.each

函数签名: test.each(items)(displayName, testFunction)

  • items <T[]>
  • displayName <(item: T) => string> 用例描述
  • testFunction <(args: TestArguments & { item: T }) => void | Promise<void> > 测试函数,参数中含有测试所需的工具方法

Generate multiple test cases.

import { test } from 'xbell';

test.each([1, 2, 3])(
(item, index) => `case data is ${item}, index is ${index}`,
({ expect, item }) => {
expect(item).toBe(index + 1);
}
);

test.batch

函数签名: test.batch(items)(displayName, testFunction)

  • items <T[]>
  • displayName <string> 用例描述
  • testFunction <(args: TestArguments & { item: T }) => void | Promise<void> > 测试函数,参数中含有测试所需的工具方法

Unlike test.each , test.batch only generates one test case, so if one item fails, this test case will fail.

import { test } from 'xbell';

test.batch([1, 2, 3])(
'item is equal to the index plus one',
({ expect, item }) => {
expect(item).toBe(index + 1);
}
);

test.todo

函数签名: test.todo(displayName, testFunction)

  • displayName <string> 用例描述
  • testFunction <(args: TestArguments) => void | Promise<void>> 测试函数,参数中含有测试所需的工具方法
test.todo('goto github', async (page) => {
await page.goto('https://github.com');
});

test.only

函数签名: test.only(displayName, testFunction)

  • displayName <string> 用例描述
  • testFunction <(args: TestArguments) => void | Promise<void> > 测试函数,参数中含有测试所需的工具方法
test.only('goto github', async (page) => {
await page.goto('https://github.com');
});

test.skip

函数签名: test.skip(displayName, testFunction)

  • displayName <string> 用例描述
  • testFunction <(args: TestArguments) => void | Promise<void> > 测试函数,参数中含有测试所需的工具方法
test.skip('goto github', async (page) => {
await page.goto('https://github.com');
});

test.browser

函数签名: test.browser(displayName, testFunction)

  • displayName <string> 用例描述
  • testFunction <(args: TestArguments) => void | Promise<void> > 测试函数,参数中含有测试所需的工具方法
import { test } from 'xbell';

test.browser('one plus one equals two', async ({ page, expect }) => {
const { add } = await import('./add.ts');
expect(add(1, 1)).toBe(2);
});

test.browser.extend

函数签名: test.browser.extend(extendFunction)

  • extendFunction <(testArgs: BrowserTestArguments) => any>

test.browser.extend 可以扩展或自定义浏览器用例的环境,它将在每个用例开始之前执行

import { test as baisc } from 'xbell';

const test = basic.browser.extend(({ expect, fn, spyOn }) => {
const data = [ 1, 2, 3, ];

window.data = data;

return {
expect,
data,
fn,
spyOn
};
});

test('browser extend demo', ({ expect, data }) => {
expect(data).toBe(window.data);
});

test.browser.each

函数签名: test.browser.each(items)(displayName, testFunction)

  • items <T[]>
  • displayName <(item: T) => string> 用例描述
  • testFunction <(args: TestArguments & { item: T }) => void | Promise<void> > 测试函数,参数中含有测试所需的工具方法

Generate multiple test cases.

import { test } from 'xbell';

test.browser.each([1, 2, 3])(
(item, index) => `case data is ${item}, index is ${index}`,
({ expect, item }) => {
expect(item).toBe(index + 1);
}
);

test.browser.batch

函数签名: test.browser.batch(items)(displayName, testFunction)

  • items <T[]>
  • displayName <string> 用例描述
  • testFunction <(args: TestArguments & { item: T }) => void | Promise<void> > 测试函数,参数中含有测试所需的工具方法

Unlike test.each , test.batch only generates one test case, so if one item fails, this test case will fail.

import { test } from 'xbell';

test.browser.batch([1, 2, 3])(
'item is equal to the index plus one',
({ expect, item }) => {
expect(item).toBe(index + 1);
}
);

test.browser.todo

函数签名: test.browser.todo(displayName, testFunction)

  • displayName <string> 用例描述
  • testFunction <(args: TestArguments) => void | Promise<void> > 测试函数,参数中含有测试所需的工具方法
test.browser.todo('goto github', async (page) => {
await page.goto('https://github.com');
});

test.browser.only

函数签名: test.browser.only(displayName, testFunction)

  • displayName <string> 用例描述
  • testFunction <(args: TestArguments) => void | Promise<void> > 测试函数,参数中含有测试所需的工具方法
test.browser.only('goto github', async (page) => {
await page.goto('https://github.com');
});

test.browser.skip

函数签名: test.browser.skip(displayName, testFunction)

  • displayName <string> 用例描述
  • testFunction <(args: TestArguments) => void | Promise<void> > 测试函数,参数中含有测试所需的工具方法
test.browser.skip('goto github', async (page) => {
await page.goto('https://github.com');
});

describe

函数签名: describe(description, testGroupFunction)

  • description : <string>
  • testGroupFunction : <() => void;>

describe.skip

函数签名: describe.skip(description, testGroupFunction)

  • description : <string>
  • testGroupFunction : <() => void;>

describe.only

函数签名: describe.only(description, testGroupFunction)

  • description : <string>
  • testGroupFunction : <() => void;>

describe.todo

函数签名: describe.todo(description, testGroupFunction)

  • description : <string>
  • testGroupFunction : <() => void;>