"""Offline Flutter Test Pyramid (Unit Test, WidgetTester pump/tap, Integration Test flow) verifier for Ch21 (test)."""
import json
VERSION = "CPython 3.12.14 offline Flutter/Dart architecture & runtime verifier (Flutter SDK absent)"
def check(name, summary, expected, actual):
return {
"name": name,
"summary": summary,
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
class MathUtils:
@staticmethod
def add(a, b):
return a + b
@staticmethod
def subtract(a, b):
return a - b
@staticmethod
def multiply(a, b):
return a * b
@staticmethod
def divide(a, b):
if b == 0:
raise ValueError("不能除以零")
return a / b
@staticmethod
def factorial(n):
if n < 0:
raise ValueError("负数没有阶乘")
if n in (0, 1):
return 1
return n * MathUtils.factorial(n - 1)
class MiniWidgetTester:
def __init__(self):
self.count = 0
self.rendered_texts = []
def pump_widget(self):
self.count = 0
self.pump()
def tap_increment(self):
self.count += 1
def pump(self):
self.rendered_texts = [f"计数: {self.count}", "增加"]
def finds_one_text(self, text):
return self.rendered_texts.count(text) == 1
def main():
cases = []
# Case 1: Unit test suite for MathUtils (arithmetic, factorial, ArgumentError checks)
div_zero_caught = False
try:
MathUtils.divide(10, 0)
except ValueError:
div_zero_caught = True
neg_fact_caught = False
try:
MathUtils.factorial(-1)
except ValueError:
neg_fact_caught = True
cases.append(
check(
"unit_test_math_utils",
"Verify MathUtils unit test assertions (add/subtract/multiply/divide/factorial & exception cases)",
{
"add": 5,
"subtract": 2,
"multiply": 20,
"divide": 5.0,
"fact_5": 120,
"fact_0": 1,
"div_zero_caught": True,
"neg_fact_caught": True,
},
{
"add": MathUtils.add(2, 3),
"subtract": MathUtils.subtract(5, 3),
"multiply": MathUtils.multiply(4, 5),
"divide": MathUtils.divide(10, 2),
"fact_5": MathUtils.factorial(5),
"fact_0": MathUtils.factorial(0),
"div_zero_caught": div_zero_caught,
"neg_fact_caught": neg_fact_caught,
},
)
)
# Case 2: Widget test simulation (pumpWidget -> tap -> pump -> findsOneWidget)
tester = MiniWidgetTester()
tester.pump_widget()
init_ok = tester.finds_one_text("计数: 0")
tester.tap_increment()
stale_before_pump = tester.finds_one_text("计数: 0")
tester.pump()
after_tap1 = tester.finds_one_text("计数: 1")
tester.tap_increment()
tester.pump()
after_tap2 = tester.finds_one_text("计数: 2")
cases.append(
check(
"widget_test_pump_and_tap",
"Verify WidgetTester pumpWidget/tap/pump frame advancement from '计数: 0' -> '计数: 1' -> '计数: 2'",
{"init_0": True, "before_pump_still_0": True, "after_tap1": True, "after_tap2": True},
{"init_0": init_ok, "before_pump_still_0": stale_before_pump, "after_tap1": after_tap1, "after_tap2": after_tap2},
)
)
# Case 3: Integration test multi-page user journey (Home -> Detail -> Back -> Home) & CLI commands
stack = ["首页"]
stack.append("详情页")
at_detail = stack[-1]
stack.pop()
back_home = stack[-1]
commands = {
"unit": "flutter test",
"widget": "flutter test",
"integration": "flutter test integration_test/",
}
cases.append(
check(
"integration_test_flow_and_commands",
"Verify integration_test navigation round-trip (首页 -> 详情页 -> 首页) and 3 flutter test commands",
{"at_detail": "详情页", "back_home": "首页", "integration_cmd": "flutter test integration_test/"},
{"at_detail": at_detail, "back_home": back_home, "integration_cmd": commands["integration"]},
)
)
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/flutter-test-lab/explore-test.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-test.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a1988.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2007.zip