2

环境:量角器,茉莉,打字稿

我有几个具有不同设置和相同断言的测试。it在 a中的每个中输入不同的步骤describeexpect在 afterEach 块中输入不同的步骤是一种好习惯吗?

示例:表单包含phoneNumberField和一个submitBtn;

测试 1:输入太短的数字phoneNumberField=> 期望submitBtn被禁用。

phoneNumberField测试 2:在=> Expect to be disabled 中输入文本submitBtn示例代码:

describe('Invalid phone number => submitBtn disabled', () => {

    afterEach(() => {
        expect(submitBtn.isEnabled()).toBeFalsy();
    });

    it('Too short number input', () => {
        phoneNumberField.sendKeys('012');
    });

    it('Text input', () => {
        phoneNumberField.sendKeys('asdf');
    });
});
4

1 回答 1

2

老实说,我很惊讶它以这种方式工作......我想说“不,这不是一个好习惯......”但在玩了几分钟后,我没有发现这样做有任何缺点它。

但我仍然不会这样做,因为如果你放在expect()it()中,很容易看到你的测试中的逻辑顺序(例如打开页面->期望 smtng;单击按钮->期望 smtng;等)

但是你可以实现它,使用它一段时间,看看它是否适合你自己

PS只是为了形象化为什么我认为这不是一个可怕的想法,因为它看起来可能是:拥有以下代码

beforeAll(async () => {expect(true).toBe(false)});

it("1", async () => {
});

it("2", async () => {
});

it("3", async () => {
});

将产生以下输出

Suite: UCare - Provider Search - 'Places' tab
    ✗ 1 (0.002 sec)
      - Expected true to be false.
          at UserContext.beforeAll (/Users/spleshakov/Documents/ui-automation/protractor/custom_implementation/test.spec.js:11:38)
          at /Users/spleshakov/Documents/ui-automation/node_modules/jasminewd2/index.js:112:25
          at new Promise (<anonymous>)
          at SimpleScheduler.promise (/Users/spleshakov/Documents/ui-automation/node_modules/selenium-webdriver/lib/promise.js:2242:12)
          at schedulerExecute (/Users/spleshakov/Documents/ui-automation/node_modules/jasminewd2/index.js:95:18)
          at promise (/Users/spleshakov/Documents/ui-automation/node_modules/selenium-webdriver/lib/promise.js:2232:22)
          at new Promise (<anonymous>)

    ✗ 2 (1 sec)
      - Expected true to be false.
          at UserContext.beforeAll (/Users/spleshakov/Documents/ui-automation/protractor/custom_implementation/test.spec.js:11:38)
          at /Users/spleshakov/Documents/ui-automation/node_modules/jasminewd2/index.js:112:25
          at new Promise (<anonymous>)
          at SimpleScheduler.promise (/Users/spleshakov/Documents/ui-automation/node_modules/selenium-webdriver/lib/promise.js:2242:12)
          at schedulerExecute (/Users/spleshakov/Documents/ui-automation/node_modules/jasminewd2/index.js:95:18)
          at promise (/Users/spleshakov/Documents/ui-automation/node_modules/selenium-webdriver/lib/promise.js:2232:22)
          at new Promise (<anonymous>)

    ✗ 3 (0.907 sec)
      - Expected true to be false.
          at UserContext.beforeAll (/Users/spleshakov/Documents/ui-automation/protractor/custom_implementation/test.spec.js:11:38)
          at /Users/spleshakov/Documents/ui-automation/node_modules/jasminewd2/index.js:112:25
          at new Promise (<anonymous>)
          at SimpleScheduler.promise (/Users/spleshakov/Documents/ui-automation/node_modules/selenium-webdriver/lib/promise.js:2242:12)
          at schedulerExecute (/Users/spleshakov/Documents/ui-automation/node_modules/jasminewd2/index.js:95:18)
          at promise (/Users/spleshakov/Documents/ui-automation/node_modules/selenium-webdriver/lib/promise.js:2232:22)
          at new Promise (<anonymous>)

可以看出,报告正在正常打印,其中每个失败都显示为单独的it()测试用例失败

于 2019-05-08T14:23:27.210 回答