在AfterEach街区中提出断言是一个好习惯吗?



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

我有几个具有不同设置和相同断言的测试。在describe中的每个it中输入不同的步骤是一个好习惯,在Aftereach块中的expect断言?

示例:表格包含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');
    });
});

老实说,我很惊讶它以这种方式起作用...我要说的是"不,这不是一个好练习……"但是我没有发现任何事情的缺点用它玩了几分钟后。

但我仍然不会这样做,因为如果您将expect()放在it()块中,则很容易在测试中看到逻辑的顺序(例如,打开Page->期望SMTNG;单击按钮 ->期待SMTNG; etc et et smtng; etc et et 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()测试用例的故障

相关内容

  • 没有找到相关文章

最新更新