如何在 cypress 中对测试场景('it()')进行分组

问题描述 投票:0回答:1

最近我能够实现我的目标,将其包含在我的测试报告中的“跳过”栏下,但是我注意到其他应该通过的测试没有出现在测试报告中。

这就是我的编码方式:

describe('My simple test class',()=>{
   let rr = true
   beforeEach(()=>{
       if(rr == true){
           expect(2).to.equal(2)
           rr = true
       } else {
          rr = false
       }
   })

   it('child1'()=>{
      if(rr == true){
            expect(4).to.equal(4)
        } else {
            cy.end      
        }
      // - - - here i wanted to have some sort of nested or another 'it() depends but I don't know how
   })

   it('not related'()=>{
      expect(5).to.equal(5)
   }) 

})
    • 现在当我在 beforeEach 中所做的断言通过时。在报告中,它仅出现在“通过”栏中 2,它指的是下面的 2 个测试用例“child1”和“不相关”。

如果我在 beforeEach 中做出的断言为 false 或不等于,那么显然 child1 将被跳过,它将出现在报告中,如“测试”列中为 2,失败为 1,跳过为 1 并且没有通过,这应该有1 并且应该指“不相关”测试。

我是cypress的新手,我不知道如何有效地分组,这样就不会影响整个测试。

 it's like this way - -
 parenta - test scenario
   - child1a - test scenario
   - child2a - test scenario
   - child3a - test scenario
parentb - test scenario
   - child1b - test scenario
   - child2b - test scenario
   - child3b - test scenario

so if parenta is by assertion turned into false or bug then all of its child will be skip but if parentb and its child are passed then in reports all should appear as follows:

Tests | Passing | Failing | Pending | Skipped
  8   |    4    |    1    |         |   3

where:
Tests is the total test available parenta + childa + parentb + childb
Passing is the total passed test cases which is the parentb and its child
Failing is 1 which refer to parenta as a test scenario that fails or returned bug
Pending is obviously zero
Skipped is 3 which refers to the child of parenta
cypress
1个回答
3
投票

重新考虑您的测试策略可能对您有用。使用条件跳过对于测试所有组件和测试用例的可靠性并没有真正的帮助。您正在运行测试,因此运行它们不应破坏任何内容,也不应造成任何伤害。

您应该测试正面和负面案例,并进一步运行每个测试案例(

it
)完全独立于所有其他测试案例。因此,当测试 A 失败时,跳过测试 B 是没有意义的。当测试失败并留下一些奇怪的状态时,请在
afterEach
生命周期方法中清理它。您可以在此处阅读有关生命周期方法的更多信息。

为了组织您的测试,它可能会帮助您使用嵌套的

describe
块:


describe('MyComponent', () => {
  it('should be some basic test' () => {
    // test something
  });

  describe('someComplexFunctionOfMyComponent', () => {
    it('should act like case A when X', () => {
      // test case A when X
    });
    it('should act like case B when Y', () => {
      // test case B when Y
    });
  });
});

© www.soinside.com 2019 - 2024. All rights reserved.