简体   繁体   English

何时为状态测试选择基于示例的测试和基于属性的测试

[英]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 的参数化测试,但使用了类固醇

Example bases testing示例基础测试

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 :缺点:

  • Usually don't cover all the edges cases, as author of Jqwik also said in presentation about EBT that “ It cant keep its promise”通常不会涵盖所有边缘情况,正如 Jqwik 的作者在关于 EBT 的介绍中所说的那样,“它无法兑现承诺”
  • Not the best option when when there are tons of combinations to cover当有大量组合需要覆盖时,这不是最佳选择
  • In example based testing we really on the gut, because of this every programmer cover the test with different test cases, in contrast PBT is kind a exhaustive.在基于示例的测试中,我们真的很直观,因此每个程序员都用不同的测试用例覆盖测试,相比之下,PBT 是一种详尽的测试。

Properties based testing基于属性的测试

Pros:优点:

  • Good for random and specific input data generation.适用于随机和特定的输入数据生成。
  • Pretty good when there are a lot of combinations of the unique test cases, for that it works just dandy.当有很多独特的测试用例组合时非常好,因为它工作得很好。
  • It is not all about exhaustive set input combinations but it's one subset of PBT这不仅仅是关于详尽的集合输入组合,而是 PBT 的一个子集

Cons:缺点:

For PBT, mainly I've observed cons are around the readability of test cases:对于 PBT,我观察到的主要缺点是测试用例的可读性:

  • Hard to read and traverse through the generated test cases as its just logs很难阅读和遍历生成的测试用例,因为它只是日志
  • What are the current inputs for the test cases, we have to look into the 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM