将断言放在afterEach块中是一种好习惯吗?

时间:2019-05-08 13:00:43

标签: protractor automated-tests webautomation protractor-net

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

我有几个测试具有不同的设置和相同的断言。 好的做法是在afterEach块的it内的每个describeexpect断言中分别输入不同的步骤吗?

示例: 表单包含phoneNumberFieldsubmitBtn;

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

测试2:在phoneNumberField中输入文本=>期望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');
    });
});

1 个答案:

答案 0 :(得分:2)

老实说,我很惊讶它能如此工作……我要说的是“不,这不是一个好习惯……”,但是在玩了一段时间之后,我没有发现这样做的任何缺点。几分钟。

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

但是您可以实施它,使用一段时间,看看它是否对您自己很好

P.S。只是想像一下为什么我认为这不是一个可怕的主意,因为它看起来像是:有下面的代码

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()测试用例显示