【问题标题】:Only run tests if a variable is larger than a value Cypress仅在变量大于值时运行测试 Cypress
【发布时间】:2020-11-30 22:23:37
【问题描述】:

如果版本在 cypress 中为某个数字或更高,我试图只运行一组测试,但我似乎无法使其正常工作。下面的代码有点抽象,但显示了整体结构:

if (version < 8203) {
  context('Skipping Tests', () => {
    it('Feature Not Available', () => {
      cy.task('log', "Skipping test. Feature not available in this version");
    });
  });
else {
  context('Testing Feature', () => {
    it('Test 1', () => {

    });
    it('Test 2', () => {

    });
    it('Test 3', () => {

    });
  });
}

我不确定这是否是解决此问题的正确方法,我尝试了几种不同的方法来构建它,包括将 if 语句放在上下文中,但看起来 cypress 忽略了 if 语句或只是继续最后一个上下文和它的功能。

【问题讨论】:

  • 您确定version 已定义并包含您期望的值吗?
  • 好吧,你用错误的方式展示了比较。实际测试情况如何?
  • @Ackroydd 啊,对不起。是的,跳过测试应该在第一个块中。抱歉,我把结构搞混了,因为我一直在尝试不同的方法来让它工作。话虽如此,当它是正确的方式时它仍然不起作用。
  • @CalebMiller 是的,我确定,我将它打印到控制台,它被解析为一个 Int。 let version; getVersion((setVersion) =&gt; { version = parseInt(setVersion.replace(/[v.]/g, '')); console.log(version); //8203 });
  • 现在有一个语法错误(缺少if { 的右括号),但修复了这个问题,并且使用version === 8203,代码的else 分支应该运行。如果它仍然无法按预期工作,请继续检查您对完整代码的假设。

标签: javascript testing cypress end-to-end


【解决方案1】:

您提到此代码是为了获取version 中的值。

let version; 
getVersion((setVersion) => {   
  version = parseInt(setVersion.replace(/[v.]/g, ''));   
  console.log(version); //8203 
});
if (version < 8203) {  ??? is this where you test ???

如果上述模式是您的测试的样子,我会说在评估 if() 之前,getVersion() 回调尚未完成。

这种结构能改善情况吗?

let version; 
getVersion((setVersion) => {   
  version = parseInt(setVersion.replace(/[v.]/g, ''));   
  console.log(version); //8203 

  if (version < 8203) {  
    ...
  }
  else {
    context('Testing Feature', () => {
      it('Test 1', () => {
      ...
    });
  }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2020-12-12
    • 1970-01-01
    • 2023-01-29
    • 2020-07-07
    相关资源
    最近更新 更多