Paparazzi
一个Android包, 能够在没有物理设备或者模拟器的情况下, 渲染应用页面.
class LaunchViewTest {
@get:Rule
val paparazzi = Paparazzi(
deviceConfig = PIXEL_5,
theme = "android:Theme.Material.Light.NoActionBar"
// ...see docs for more options
)
@Test
fun launchView() {
val view = paparazzi.inflate<LaunchView>(R.layout.launch)
// or...
// val view = LaunchView(paparazzi.context)
view.setModel(LaunchModel(title = "paparazzi"))
paparazzi.snapshot(view)
}
@Test
fun launchComposable() {
paparazzi.snapshot {
MyComposable()
}
}
}
任务
$ ./gradlew sample:testDebug
运行测试并且在sample/build/reports/paparazzi/
生成HTML报告, 报告会显示全部测试的运行的快照.
$ ./gradlew sample:recordPaparazziDebug
将快照保存为黄金值到预定义的受源代码管理的位置(默认保存到src/test/snapshots
).
$ ./gradlew sample:verifyPaparazziDebug
运行测试并检验之前记录的黄金值. 失败的话会在sample/out/failures
生成diff文件.
要想查看更多的信息, 请查看sample项目.
Git LFS
推荐使用Git LFS保存快照. 下面是快速设置:
$ brew install git-lfs
$ git config core.hooksPath # optional, confirm where your git hooks will be installed
$ git lfs install --local
$ git lfs track "**/snapshots/**/*.png"
$ git add .gitattributes
在CI上, 可以像这样设置:
$HOOKS_DIR/pre-receive
# compares files that match .gitattributes filter to those actually tracked by git-lfs
diff <(git ls-files ':(attr:filter=lfs)' | sort) <(git lfs ls-files -n | sort) >/dev/null
ret=$?
if [[ $ret -ne 0 ]]; then
echo >&2 "This remote has detected files committed without using Git LFS. Run 'brew install git-lfs && git lfs install' to install it and re-commit your files.";
exit 1;
fi
your_build_script.sh
if [[ is running snapshot tests ]]; then
# fail fast if files not checked in using git lfs
"$HOOKS_DIR"/pre-receive
git lfs install --local
git lfs pull
fi
Jetifier
如果在使用Jetifier迁移Support包, 将下面的内容添加到gradle.properties
文件来排除捆绑的Android依赖.
android.jetifier.ignorelist=android-base-common,common
Releases
变更日志保留了发版的历史.
应用插件:
buildscript {
repositories {
mavenCentral()
google()
}
dependencies {
classpath 'app.cash.paparazzi:paparazzi-gradle-plugin:1.0.0'
}
}
apply plugin: 'app.cash.paparazzi'
使用插件DSL:
plugins {
id 'app.cash.paparazzi' version '1.0.0'
}
开发版的快照在Sonatype的快照
仓库.
repositories {
// ...
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
包名 app.cash.paparazzi
类型
名称 | 简介 |
---|---|
DeviceConfig | [jvm] 数据类DeviceConfig(val screenHeight: Int = 1280, val screenWidth: Int = 768, val xdpi: Int = 320, val ydpi: Int = 320, val orientation: ScreenOrientation = ScreenOrientation.PORTRAIT, val uiMode: UiMode = UiMode.NORMAL, val nightMode: NightMode = NightMode.NOTNIGHT, val density: Density = Density.XHIGH, val fontScale: Float = 1.0f, val layoutDirection: LayoutDirection = LayoutDirection.LTR, val locale: String? = null, val ratio: ScreenRatio = ScreenRatio.NOTLONG, val size: ScreenSize = ScreenSize.NORMAL, val keyboard: Keyboard = Keyboard.NOKEY, val touchScreen: TouchScreen = TouchScreen.FINGER, val keyboardState: KeyboardState = KeyboardState.SOFT, val softButtons: Boolean = true, val navigation: Navigation = Navigation.NONAV, val released: String = "November 13, 2012") 为多样的设备提供了 FolderConfiguration 和 HardwareConfig. 也提供了通用函数来解析build.prop 和attrs.xml 以生成恰当的map. |
Environment | [jvm] 数据类Environment(val platformDir: String, val appTestDir: String, val resDir: String, val assetsDir: String, val packageName: String, val compileSdkVersion: Int, val resourcePackageNames: List<String>) |
Flags | [jvm] 对象Flags |
HtmlReportWriter | [jvm] 类HtmlReportWriter@JvmOverloadsconstructor(runName: String = defaultRunName(), rootDirectory: File = File("build/reports/paparazzi"), snapshotRootDirectory: File = File("src/test/snapshots")) : SnapshotHandler 创建了HTML报告, 避免向已经写入的文件继续写入. |
Paparazzi | [jvm] 类Paparazzi@JvmOverloadsconstructor(environment: Environment = detectEnvironment(), deviceConfig: DeviceConfig = DeviceConfig.NEXUS_5, theme: String = "android:Theme.Material.NoActionBar.Fullscreen", renderingMode: SessionParams.RenderingMode = RenderingMode.NORMAL, appCompatEnabled: Boolean = true, maxPercentDifference: Double = 0.1, snapshotHandler: SnapshotHandler = determineHandler(maxPercentDifference), renderExtensions: Set<RenderExtension> = setOf(), supportsRtl: Boolean = false) : TestRule |
RenderExtension | [jvm] 接口RenderExtension 用于在每个渲染帧顶部叠加附加信息的扩展. |
Snapshot | [jvm] @JsonClass(generateAdapter = true) data class Snapshot(val name: String?, val testName: TestName, val timestamp: Date, val tags: List<String> = listOf(), val file: String? = null) |
SnapshotHandler | [jvm] 接口SnapshotHandler : Closeable |
SnapshotVerifier | [jvm] 类SnapshotVerifier@JvmOverloadsconstructor(maxPercentDifference: Double, rootDirectory: File = File("src/test/snapshots")) : SnapshotHandler |
TestName | [jvm] 数据类TestName(val packageName: String, val className: String, val methodName: String) |
函数
名称 | 简介 |
---|---|
androidHome | [jvm] fun androidHome(): String |
detectEnvironment | [jvm] fun detectEnvironment(): Environment |
作者:bytebeats
链接:https://juejin.cn/post/7147491565047398408
网友评论