FletTestApp
Flet app test controller coordinates running a Python-based Flet app alongside a Flutter integration test.
This class launches the Python Flet app, starts the Flutter test process, and facilitates programmatic interaction with the app's controls for automated UI testing.
Parameters:
- flutter_app_dir (os.PathLike) - Path to the Flutter app directory containing integration tests.
- flet_app_main (AppCallable | None, default:
None) - A callable or coroutine function representing the main entry point of the Flet app under test. This will be invoked with aPageinstance when the app starts. - assets_dir (os.PathLike | None, default:
None) - Path to the directory containing static assets for the Flet app. Defaults to"assets"if not provided. - test_path (str | None, default:
None) - Path to the Python test file. Used to determine the location for golden screenshot comparisons. - tcp_port (int | None, default:
None) - TCP port to run the Flet server on. If not specified, a free port is automatically selected. - test_platform (str | None, default:
None) - Target platform for the Flutter integration test (e.g.,"windows","linux","macos","android","ios"). Env override:FLET_TEST_PLATFORM. - test_device (str | None, default:
None) - Target device ID or name for the Flutter integration test. Env override:FLET_TEST_DEVICE. - capture_golden_screenshots (bool, default:
False) - IfTrue, screenshots taken during tests are stored as golden reference images. Env override:FLET_TEST_GOLDEN=1. - screenshots_pixel_ratio (float, default:
2.0) - Device pixel ratio to use when capturing screenshots. Env override:FLET_TEST_SCREENSHOTS_PIXEL_RATIO. - screenshots_similarity_threshold (float, default:
99.0) - Minimum percentage similarity required for screenshot comparisons to pass. Env override:FLET_TEST_SCREENSHOTS_SIMILARITY_THRESHOLD. - use_http (bool, default:
False) - IfTrue, use HTTP transport instead of TCP for Flet client-server communication. Env override:FLET_TEST_USE_HTTP=1. - disable_fvm (bool, default:
False) - IfTrue, do not invokefvmwhen running the Flutter test process. Env override:FLET_TEST_DISABLE_FVM=1. - skip_pump_and_settle (bool, default:
False) - IfTrue, the initialpump_and_settleafter app start is skipped. - device_mode (bool, default:
False) - IfTrue, the app under test runs on-device with embedded Python (over dart_bridge), as produced byflet build. This session then hosts only theTesterover a dedicated channel and does not runflet_app_main. IfFalse(default), the app runs here in-process (host mode) against the devclientshell. Env override:FLET_TEST_DEVICE_MODE=1.
Environment Variables:
FLET_TEST_PLATFORM: Overridestest_platform.FLET_TEST_DEVICE: Overridestest_device.FLET_TEST_GOLDEN: Enables golden screenshot capture when set to1.FLET_TEST_SCREENSHOTS_PIXEL_RATIO: Overridesscreenshots_pixel_ratio.FLET_TEST_SCREENSHOTS_SIMILARITY_THRESHOLD: Overridesscreenshots_similarity_threshold.FLET_TEST_USE_HTTP: Enables HTTP transport when set to1.FLET_TEST_DISABLE_FVM: Disablesfvmusage when set to1.
Properties
page- Returns an instance of Flet's appPage.screenshots_pixel_ratioscreenshots_similarity_thresholdtest_devicetest_platformtester- Returns the tester that programmatically interacts with page controls and the test environment.
Methods
assert_control_screenshot- Adds control to a clean page, takes a screenshot and compares it with a golden copy or takes golden screenshot ifFLET_TEST_GOLDEN=1environment variable is set.assert_gif- Compare an animated GIF built fromframesagainst a golden GIF.assert_screenshot- Compares provided screenshot with a golden copy or takes golden screenshot ifFLET_TEST_GOLDEN=1environment variable is set.create_gif- Create an animated GIF from a sequence of PNG frames.resize_page- Resizes the page window to the specified width and height.start- Starts Flet app and Flutter integration test process.take_page_controls_screenshot- Takes a screenshot of all controls on the current page.teardown- Teardown Flutter integration test process.wrap_page_controls_in_screenshot- Wraps provided controls in a Screenshot control.
Properties
screenshots_pixel_ratioinstance-attribute
screenshots_similarity_thresholdinstance-attribute
test_deviceinstance-attribute
test_platforminstance-attribute
Methods
assert_control_screenshotasync
assert_control_screenshot(
name: str,
control: Control,
pump_times: int = 0,
pump_duration: DurationValue | None = None,
expand_screenshot: bool = False,
similarity_threshold: float = 0,
)Adds control to a clean page, takes a screenshot and compares it with a golden copy or takes golden screenshot if FLET_TEST_GOLDEN=1 environment variable is set.
Parameters:
- name (str) - Screenshot name - will be used as a base for a screenshot filename.
- control (Control) - Control to take a screenshot of.
assert_gif
assert_gif(
name: str,
frames: Iterable[bytes],
duration: int | Sequence[int] = 1000,
loop: int = 0,
disposal: DisposalMode = DisposalMode.DEFAULT,
similarity_threshold: float = 0,
)Compare an animated GIF built from frames against a golden GIF.
Builds the GIF in memory from the provided frames. If the
FLET_TEST_GOLDEN=1 environment variable is set, writes the GIF as
the golden reference. Otherwise loads the existing golden GIF from
disk and compares frame-by-frame via structural similarity, saving
an <name>_actual.gif next to the golden on mismatch.
Parameters:
- name (str) - GIF name - will be used as a base for the GIF file name.
- frames (Iterable[bytes]) - Iterable of PNG-encoded frame bytes. Typically the result of
Page.take_animation(...). - duration (int | Sequence[int], default:
1000) - Frame duration in milliseconds. Either a singleintor a per-frame sequence matching the number of frames. - loop (int, default:
0) - Number of times the GIF should repeat (0means infinite). - disposal (DisposalMode, default:
DisposalMode.DEFAULT) - Frame disposal mode. - similarity_threshold (float, default:
0) - Minimum acceptable per-frame SSIM (%). Usesscreenshots_similarity_thresholdwhen0.
assert_screenshot
assert_screenshot(
name: str,
screenshot: bytes,
similarity_threshold: float = 0,
)Compares provided screenshot with a golden copy or takes golden screenshot if FLET_TEST_GOLDEN=1 environment variable is set.
Parameters:
- name (str) - Screenshot name - will be used as a base for a screenshot filename.
- screenshot (bytes) - Screenshot contents in PNG format.
create_gif
create_gif(
image_names: Iterable[str] | None = None,
output_name: str = "",
frames: Iterable[bytes] | None = None,
duration: int | Sequence[int] = 1000,
loop: int = 0,
disposal: DisposalMode = DisposalMode.DEFAULT,
) -> PathCreate an animated GIF from a sequence of PNG frames.
Exactly one of image_names or frames must be provided. Unlike
assert_gif, this method only writes the GIF and returns its
path; it does not compare against a golden file.
Parameters:
- image_names (Iterable[str] | None, default:
None) - Iterable of file name stems (without.png) in the order they should appear in the animation. Frames are read from disk under the test's golden directory. - output_name (str, default:
'') - Base name for the resulting animation. The.gifextension is added automatically and the file is stored in the same directory as the source frames. - frames (Iterable[bytes] | None, default:
None) - Iterable of PNG-encoded frame bytes to use directly, in the order they should appear in the animation. Typically paired withtake_animation. - duration (int | Sequence[int], default:
1000) - Frame duration in milliseconds. Either a singleintapplied to every frame, or a sequence ofintwith one entry per frame. Pass the same list used fortake_animation(frame_delays_ms=...)to have the GIF play at the same pace it was captured. - loop (int, default:
0) - Number of times the GIF should repeat (0means infinite). - disposal (DisposalMode, default:
DisposalMode.DEFAULT) - Frame disposal mode.
Returns:
- Path - Path to the generated GIF file.
Raises:
- ValueError - If neither or both of
image_names/framesare given, the input is empty, or adurationsequence has a different length than the frame count. - FileNotFoundError - If any referenced image file does not exist.
resize_page
resize_page(width: float, height: float)Resizes the page window to the specified width and height.
take_page_controls_screenshotasync
take_page_controls_screenshot(
pixel_ratio: float | None = None,
pump_times: int = 0,
pump_duration: DurationValue | None = None,
) -> bytesTakes a screenshot of all controls on the current page.
wrap_page_controls_in_screenshotasync
wrap_page_controls_in_screenshot(
margin=10,
pump_times: int = 0,
pump_duration: DurationValue | None = None,
) -> ScreenshotWraps provided controls in a Screenshot control.