This document follows RFC 2119.
Finding Widgets
PREFER using keys to find widgets
Patrol’s custom finders are very powerful, and you might often be inclined to find the widget you want in a variety of ways. While we’re encouraging you to explore and play with Patrol’s custom finders, we are quite confident that keys are the best way to find widgets.Why not strings?
Why not strings?
At first, strings might seem like a good way to find widgets.They’ll get increasingly annoying to work with as your app grows and changes, for example, when the strings in your app change.Using strings stops making any sense when you have more than 1 language in your app. Using strings in such case is asking for trouble.
Why not classes?
Why not classes?
There are 2 problems with using classes.First is that they hurt your test’s readability. You want to tap on the login button or enter text into the username field. You don’t want to tap on, say, the third button and enter text into the second text field.The second problem is that classes are almost always an implementation detail. As a tester, you shouldn’t care if something is a
TextButton or an OutlineButton. You care that it is the login button, and you want to tap on it. In most cases, that login button should have a key.Example: Bad Practice
Example: Good Practice
If you assigned a key to the login button, the above could be simplified to:CONSIDER having a file where all keys are defined
The number of keys will get bigger as your app grows and you write more tests. To keep track of them, it’s a good idea to keep all keys in, say,lib/keys.dart file.
lib/keys.dart
In app UI code
In app test code
This is a good way to make sure that the same keys are used in app and tests. No more typos!
Test Structure
PREFER having one test path
Good tests test one feature, and test it well (this applies to all tests, not only Patrol tests). This is often called the “main path”. Try to introduce as little conditional logic as possible to help keep the main path straight. In practice, this usually comes down to having as fewifs as possible.
Keeping your test code simple and to the point will also help you in debugging it.
DO add a good test description explaining the test’s purpose
If your app is non-trivial, your Patrol test will become long pretty quickly. You may be sure now that you’ll always remember what the 200 line long test you’ve just written does and are (rightfully) very proud of it. Believe us, in 3 months you will not remember what your test does. This is why the first argument topatrolTest is the test description. Use it well!
Additional Best Practices
Test the “Happy Path”
UI tests (integration tests), like the ones we’re writing with Patrol, should focus on testing the “happy path” of a UI flow. We only want them to fail if the app suddenly stops the user from doing what the app is for.Validation error messages are not “what the app is for” - they exist only to allow the user to successfully complete their task. Don’t test validation errors in integration tests; use unit tests for that instead.