leancodepl / patrol

Flutter-first UI testing framework. Ready for action!
https://patrol.leancode.co
Apache License 2.0
904 stars 139 forks source link

[Question] How to run `patrolTest` with variants that come from `late` fields initialized during `setUp`? #2276

Closed ReubenTurner-esusu closed 3 months ago

ReubenTurner-esusu commented 3 months ago

Using variants like so works:

void main() {
  patrolTest(
    'Verify login works',
    variant: DevUsers.variants,
    ($) async {
      // test content
    },
  );
}

class DevUsers {
  static ValueVariant<User> variants = ValueVariant({user1, user2});

  // users 1 & 2 defined here
}

However, taking the variants from a late value initialized during setUp does not work:

void main() {
  late PatrolOrchestrator patrolOrchestrator;

  setUp(() {
    patrolOrchestrator = PatrolOrchestrator();
  });

  patrolTest(
    'Verify login works',
    variant: patrolOrchestrator.userVariants,
    ($) async {
      // test content
    },
  );
}

// Slimmed-down version of our class.
class PatrolOrchestrator {
  PatrolOrchestrator() {
    flavor = switch (const String.fromEnvironment('ENV')) {
      'staging' => AppFlavor.staging,
      'production' => AppFlavor.production,
      _ => AppFlavor.development,
    };
    userVariants = switch (flavor) {
      AppFlavor.development => DevUsers.variants,
      AppFlavor.staging => StagingUsers.variants,
      AppFlavor.production => ProdUsers.variants,
    };
  };

  late AppFlavor flavor;
  late ValueVariant<User> userVariants;
}

The error given for why this doesn't work is:

Unhandled Exception: LateInitializationError: Local 'patrolOrchestrator' has not been initialized.

This is quite odd because patrolOrchestrator works just fine in all of our test bodies; it is only when referencing it like in the second example above that there's a problem. Using setUpAll or putting the setUp and test inside a group doesn't help.

I have also tested this problem against very simple values like Strings. late ValueVariants initialized in setUp cause tests to be skipped.

What is the correct way to accomplish this?

ReubenTurner-esusu commented 3 months ago

Follow-up: doing the following:

void main() {
  // late PatrolOrchestrator patrolOrchestrator;

  // setUp(() {
  //   patrolOrchestrator = PatrolOrchestrator();
  // });

  group('login', () {
    final patrolOrchestrator = PatrolOrchestrator();
    patrolTest(
      'Verify login works',
      variant: patrolOrchestrator.userVariants,
      ($) async {
        // Test content here
      },
    );
  });
}

allows the tests to run, and I can see the tests succeeding. However, the following error is produced for each time the test is run:

: Starting 2 tests on SM-N986U1 - 13
        : 
        : com.esusu.consumerapp.MainActivityTest > runDartTest[login.login_test login Verify login works (variant: Instance of 'User')][SM-N986U1 - 13] FAILED 
        :       java.lang.RuntimeException: pl.leancode.patrol.contracts.PatrolAppServiceClientException: Invalid response 500, Internal Server Error
        :       at pl.leancode.patrol.PatrolJUnitRunner.runDartTest(PatrolJUnitRunner.java:140)
        : 
        : com.esusu.consumerapp.MainActivityTest > runDartTest[login.login_test login Verify login works (variant: Instance of 'User')][SM-N986U1 - 13] FAILED 
        :       java.lang.RuntimeException: pl.leancode.patrol.contracts.PatrolAppServiceClientException: Invalid response 500, Internal Server Error
        :       at pl.leancode.patrol.PatrolJUnitRunner.runDartTest(PatrolJUnitRunner.java:140)
        : Tests on SM-N986U1 - 13 failed: There was 2 failure(s).
        : 
        : > Task :app:connectedDevelopmentDebugAndroidTest FAILED

So in this version the test actually fails despite the eye-test showing it succeeding

ReubenTurner-esusu commented 3 months ago

Closing this as setUp only runs before the test body

fylyppo commented 3 months ago

Hi, yes, setUp runs once before each test in group starts. But also, it's better to usepatrolSetUp instead of setUp. It's supported with native automation.

ReubenTurner-esusu commented 3 months ago

Hi, yes, setUp runs once before each test in group starts. But also, it's better to usepatrolSetUp instead of setUp. It's supported with native automation.

Interesting, I was not aware of patrolSetUp. I assume that also only runs before the test body?

github-actions[bot] commented 3 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar problem, please file a new issue. Make sure to follow the template and provide all the information necessary to reproduce the issue.