[英]When to choose Example based testing and property based for Stateful Testing
I'm doing unit testing mostly these days for Android SDK in Android Studio and using Jqwik which is a Property-Based testing tool on the JUnit Platform.这些天我主要在 Android Studio 中为 Android SDK 进行单元测试,并使用Jqwik ,它是 JUnit 平台上基于属性的测试工具。
While exploring the different test techniques approach with my seniors, I learned about example-based testing and property-based testing.在与我的前辈一起探索不同的测试技术方法时,我了解了基于示例的测试和基于属性的测试。 I want to know when to choose which one.
我想知道什么时候选择哪一个。
Purpose of sharing this : To double-check with the community if I'm thinking in the right direction.分享此内容的目的:与社区再次确认我的想法是否正确。
Notations: EBT = Example-Based Testing;符号:EBT = 基于示例的测试; PBT = Property-Based Testing;
PBT = 基于属性的测试;
What I understood : EBT is done to check if features are working as they are supposed to be, while PBT takes to one level above, by falsifying the given preconditions(inputs) against the invariants(domain behavior)我的理解:EBT 是为了检查功能是否按预期工作,而 PBT 通过针对不变量(域行为)伪造给定的先决条件(输入)来达到更高的水平
What I feel : I feel like the Jqwik tool is like a parameterized test of JUnit but on steroids我的感受:我觉得 Jqwik 工具就像 JUnit 的参数化测试,但使用了类固醇
Pros:优点:
Easy visibility of which test passes with their specified names by the programmer in contrast to PBT just shows the total count of tries tho we can log the generated test cases but the JUnit way to is more visually appealing and easy to traverse and read through与 PBT 相比,程序员可以轻松查看哪些测试以其指定的名称通过,仅显示尝试的总次数,我们可以记录生成的测试用例,但 JUnit 方式更具视觉吸引力并且易于遍历和阅读
Can easily read and understand the inputs as mentioned in the test case itself in contrast to PBT where we have to look into the Provider method from where Jqwik get all the Arbitraries.可以很容易地阅读和理解测试用例中提到的输入,这与 PBT 相比,我们必须查看 Jqwik 从中获取所有 Arbitraries 的 Provider 方法。
Good for testing state machine where there are not any combination of dependencies.适合测试没有任何依赖组合的状态机。
Might be easy for the Non-Tech person to understand the working of state machine if he knows how that domain component works如果非技术人员知道该域组件的工作原理,那么他可能很容易理解状态机的工作原理
Cons :缺点:
Pros:优点:
Cons:缺点:
For PBT, mainly I've observed cons are around the readability of test cases:对于 PBT,我观察到的主要缺点是测试用例的可读性:
Provides
annotation functions.Provides
注释功能。For me it's usually not an either-or question, but examples and properties often complement each other.对我来说,这通常不是一个非此即彼的问题,但示例和属性通常相互补充。 Whereas examples are good starting points for the intended behaviour of the code under test, properties serve to give me sufficient trust in the breadth of functionality and expected behaviour in edge and corner cases.
虽然示例是测试代码的预期行为的良好起点,但属性使我对功能的广度和边缘和极端情况下的预期行为有足够的信任。
There are situations where examples are better replaced by properties, eg when many examples can be translated into a single or just a few properties.在某些情况下,示例可以更好地替换为属性,例如,当许多示例可以转换为单个或仅几个属性时。 Even then I tend to keep some examples, because they are easier to understand.
即便如此,我还是倾向于保留一些例子,因为它们更容易理解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.