Skip to content

Refactorings to testbed.run and testbed.test#44488

Merged
jonahwilliams merged 1 commit intoflutter:masterfrom
jonahwilliams:refactor_testbed
Nov 12, 2019
Merged

Refactorings to testbed.run and testbed.test#44488
jonahwilliams merged 1 commit intoflutter:masterfrom
jonahwilliams:refactor_testbed

Conversation

@jonahwilliams
Copy link
Contributor

Description

Attempts to reduce testing and simplify how tests are declared and setup, especially in the presence of NNBD. Changes:

  • (1) Create testbed in main instead of setup. Rationale: creating a testbed does not perform any additional work beyond object creation. Moving into setup to avoid work for the case where we only want to run a single test doesn't buy us much, and would force us to either declare testbed as late or nullable - both of which introduce potential for runtime exceptions and overhead.

  • (2) Remove top level fields referenced in setUp. The mockito APIs do not require access to instance itself - we can use the typed context getters instead.

  • (3) Remove nested closure and provide test method on testbed itself. Reduces nesting, looks cleaner, and no regression in functionality due to annotation. Requires (1).

If this seems like a nicer approach to take, I'll convert the reset of the APIs and remove run.

@fluttergithubbot fluttergithubbot added the tool Affects the "flutter" command-line tool. See also t: labels. label Nov 9, 2019
@jonahwilliams
Copy link
Contributor Author

One of the situations I want to avoid in a post NNBD world:

void main() {
  Foo? foo;
  Bar? bar;

  setUp(() {
    foo = TestFoo();
    bar = TestBar();
  });
  
  test('example', () {
    foo!.doA();
    foo!.doB();
    bar!.doC();
  });
}


test('Can run a build', () => testbed.run(() async {
when(mockBuildSystem.build(any, any, buildSystemConfig: anyNamed('buildSystemConfig')))
testbed.test('Can run a build', () async {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my own understanding, how is testbed different than using testUsingContext?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testbed allows you to supply a setup function which is invoked in the same Zone that the test case is run in. This means that the zone injected values will be exactly the same (logger, fs, buildSystem).

The only way to do this setup with test usingContext is to use top levels initialized in setup and the test case. This requires plumbing values between various setup functions and the test callbacks, which is where nullability issues creep in.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, running it in the same zone allows you to use the same affordances for setup that you would otherwise use for programming. Example:

before

FileSystem fileSystem;

setUp(() {
  fileSystem = MemoryFileSystem(fileSystemStyle: ...);
  fileSystem.file('a').createSync();
});

testUsingContext('a exists', () {
  expect(fs.file('a).existsSync(), true);
}, overrides: <Type, Generator>{
  FileSystem: () => fileSystem,
}

after

Testbed testbed = Testbed(setup: () {
  fs.file('a').createSync();
});

testbed.test('a exists, () {
  expect(fs.file('a).existsSync(), true);
});

Copy link
Contributor

@christopherfujino christopherfujino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jonahwilliams jonahwilliams merged commit 7caef21 into flutter:master Nov 12, 2019
@jonahwilliams jonahwilliams deleted the refactor_testbed branch November 12, 2019 23:36
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants