'Assertion from a test group lets tests fail from another group. How to prevent?

Within my Dart unit tests I have multiple groups. One of these groups has an assertion that checks some conditions which are required for tests of that group. Tests from other groups do not need that assertion. That's why I placed them in another group. The problem is that if tests from these other groups are run they fail because that assertion from that other group fails even if those tests are not in the scope of the run.

Here's an example. The test some other test from the group some other group is executed but fails because the assertion from the group group with failing assertion fails.

import 'package:test/test.dart';

void main() {

  group('group with failing assertion', () {
    assert(false, 'this fails for demonstrating purposes');
    test('some test', () {
      expect(true, equals(true));
    });
  });
  
  group('some other group', () {
    test('some other test', () {
      expect(true, equals(true));
    });
  });

}

To try it out yourself run dart test -N "some other test" or dart test -N "some other group".

How do I prevent this? How do I make sure that tests from one group do not fail because some assertion from another group fails?

Is this designed by intention? Or is it a bug?



Solution 1:[1]

From dart-lang/test GitHub repo:

It should work if you move the assertion inside of a call to setUp (or setUpAll):

import 'package:test/test.dart';

void main() {

  group('group with failing assertion', () {
    setUpAll(() {
      assert(false, 'this fails for demonstrating purposes');
    });
    test('some test', () {
      expect(true, equals(true));
    });
  });
  
  group('some other group', () {
    test('some other test', () {
      expect(true, equals(true));
    });
  });
}

You should only have failures inside of test, setUp[All], or tearDown[All] functions.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 0x4b50