【问题标题】:Is there a way to skip all tests in a suite if a certain conditions fail - JavaScript Jasmine?如果某些条件失败 - JavaScript Jasmine,有没有办法跳过套件中的所有测试?
【发布时间】:2021-11-22 16:44:16
【问题描述】:

如果某些条件失败,有没有办法跳过套件中的所有测试? 即)如果网页未打开,则运行其余测试毫无意义,因为它们都依赖于在运行任何测试之前打开的网页。

我们可以使用 pending() 跳过当前测试,但是如果所有其他测试都依赖 checkCondition 为真,有没有办法立即跳过套件中的所有测试或下面的测试?

我尝试在 beforeAll 块中添加 pending(),以尝试跳过所有测试,因为 beforeAll 在任何东西之前运行。

请帮忙!我正在使用 WebdriverIO 和 Jasmine。谢谢!

let checkCondition = false;

describe(`My Test Suite`, () => {
  beforeAll(async () => {
     // EDIT - returnBoolean() is another method that logs into the 
     // page and returns true or false
     let setCheckCondition = returnBoolean();

    if (setCheckCondition) {
      checkCondition = true;
      console.log(`in true block`);
    } else {
      console.log('Skip tests below');  // HELP <-- since checkCondition is false, all tests below should fail 
      pending();
    }
  });

  it(`Test 1`, () => {
    if (checkCondition != undefined) {
      console.log("checkCOndition is defined")
    } else {
      pending();
    }
  });

  it(`Test 2`, () => {
    if (checkCondition) {
      // check 
    } else {
      console.log('Skip this test');
      pending();
    }
  });

  it(`Test 3`, () => {
    console.log("skip this test too")
  });

});

【问题讨论】:

    标签: javascript testing jasmine webdriver-io wdio-jasmine


    【解决方案1】:

    如果您必须在beforeAll 中设置checkCondition,然后最终从那里中止,那么 github 上似乎存在一个持续存在的问题 (https://github.com/jasmine/jasmine/issues/1533)

    否则,如果 checkConditon 可以在测试套件开始之前知道(我不明白为什么不知道),您可以将您的 beforeAll 替换为类似

    if (!checkCondition) return
    

    或者一开始就跳过对describe的整个调用

    编辑: 您可以像这样跳过整个测试套件:

    let checkCondition = returnBoolean();
    if (checkCondition) {
      describe(`My Test Suite`, () => {
        it(`Test 1`, () => {
          // run test 1 normally
        });
      
        it(`Test 2`, () => {
          // run test 2 normally
        });
      
        it(`Test 3`, () => {
          // run test 3 normally
        });
      
      });
    } else {
      console.log("checkCondition is false. Skipping all tests!");
    }
    

    【讨论】:

    • 谢谢!我意识到我错过了一个额外的条件,所以我编辑了代码。有一个 setCheckCondition 检查,如果这是真的,那么只有当 setCheckCondition 首先为真时,checkCondition 才会被设置为真。我不能将 if 放在 'it' 块之前,否则执行顺序将不正确。你有什么其他想法,因为我不能用 if 检查替换 beforeAll 吗?谢谢
    • 如果 checkCondition 为 false,您希望跳过所有测试。所以这意味着你根本不想运行 it 块。我在正确的轨道上吗?
    • yes checkCondition 可以为真或假,具体取决于 setCheckCondition 的结果。如果 checkCondition 为 false,则跳过所有测试。如果 checkCondition 为真,则运行所有测试
    • 好的,我会更新我的答案
    猜你喜欢
    • 2019-09-12
    • 2020-02-01
    • 2012-07-12
    • 2015-08-09
    • 1970-01-01
    • 1970-01-01
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多