问题标签 [fscheck]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
unit-testing - 我将如何对纸牌游戏的发牌功能进行基于属性的测试?
我正在研究基于属性的测试,并且很好奇如何将这种类型的测试应用到 BlackJack 游戏的 Deal 函数中。
这是一个单元测试(又名:基于示例的测试):
什么是基于属性的测试的示例?
我会测试两张牌是否在 4 套花色范围内吗?(即黑桃、方块、红心、梅花) 依赖静态类型不会对我来说已经确保了吗?
我会测试两张牌是否在数字或面的范围内?(即 2..10;Jack;Queen;King;Ace) 再一次,依赖静态类型不是已经为我确保了吗?
同样,在为二十一点游戏执行基于属性的测试时,我应该考虑什么?
.net - 如果随机生成的输入没有用,我如何重新尝试基于属性的测试?
我是单元测试的n00b。我已经从 Nuget 安装了FsCheck.Nunit
,NUnitTestAdapter
我正在尝试进行基于属性的测试,这在很大程度上受到了不可估量的 Scott Wlaschin 的启发。
我正在使用该[<Property>]
属性,并且我希望能够“跳过”不符合测试要求的输入:
最简单的方法是什么?
如果 FsCheck/NUnit 存在,我更喜欢它的答案,但我也会考虑其测试可以在 Visual Studio 中运行的任何其他框架。(我以为我看到了一些框架,其中有一个简单的函数可以做到这一点,但我不知道它是什么。)
到目前为止,我更喜欢 FsCheck.NUnit,因为它可以为 F# 类型(可区分联合等)生成随机输入,而无需额外的工作。
c# - 在 FsCheck 中生成唯一的字符串
我需要生成唯一的非null
字符串用作Dictionary
键。我试过类似的东西:
然后我用UniqueString()
在:
但是,我在属性测试中遇到异常,Foo
因为 FsCheck 有时会两次生成相同的字符串,从而导致DuplicateKeyException
.
如何生成要传递给构造函数的唯一字符串Foo
?
f# - FsCheck.xUnit:测试来自另一个程序集的类型
我正在尝试使用 FsCheck 和 xUnit 进行我的第一次测试。我有以下设置:
具有单个文件的 F# 类库
/li>一个名为 Tests 的项目引用了 FsCheck.xUnit NuGet 包和单个文件:
/li>
当我运行项目(使用 VS 或控制台运行程序)时,出现以下错误:
如果我将我的类型移动到测试程序集,一切正常(测试失败)。如何测试外部类型?
f# - 用于 Gen 样本的尺寸值是否采用自定义生成器?
我正在使用FsCheck
's生成自定义数据Gen
。
假设你有一个函数返回Gen<'T>
:
然后用于生成数据:
是size
在这种情况下使用的参数(第一个),它是否影响值重新分配?
c# - 如何在 C# 中为接口编写 FsCheck 生成器
假设我有一个接口 IPerson,它有 2 个读取属性 age (int) 和 name (string)。
我还有一个实现 IPerson 的类 Person。
如何编写 FsCheck 生成器来生成 IPerson 类型的实例?
mocking - 如何在 F# 中将 xUnit 和 FsCheck 与 IoC 和模拟一起使用
我想使用属性基础测试技术编写单元测试 F#。但是我遇到了几个障碍。
- 我要测试的代码是 C#
- 域对象来自 EF,即没有构造函数,只有可变属性
- sut 是一个需要大量构造函数注入的类。随着我们添加新功能,它们的数量会经常变化。当我们向构造函数添加新参数时,我们不想更改旧测试。
- sut 已经存在并且正在工作,因此将其模式更改为在构造函数中仅采用一个工厂参数将被视为测试引起的损坏,因此这是不行的
- FsCheck 无法生成开箱即用的实体框架对象,因此我们不得不为每个实体编写大型生成器 - 是否有任何库可以为我们完成此任务或使这项任务变得更容易?
换句话说,是否可以将 FsCheck 与 Ninject(我们在应用程序中使用它,因此添加新的也行不通)与一些自动实体生成器和模拟工具结合起来?
entity-framework - 生成随机对象作为测试用例
这个问题是更大问题的一部分,可以在这里找到
我们有来自实体框架的类。换句话说,它们不是不可变的记录,它们是没有构造函数的可变属性列表。FsCheck 无法开箱即用地处理此类实体,并且对于每个实体,我们被迫编写单独的生成器,如下所示:
正如您可以想象的那样,编写这样的东西的需要可能会阻止人们编写这种单元测试。
如何在 FsCheck 中为 C# EF 实体自动生成测试用例。
quickcheck - 使用基于属性的测试工具生成事件/命令?
据我了解,大多数属性测试工具都是在功能级别上运行的。给定一组参数,此类工具将针对某些不变量生成随机输入和测试输出。
我已经读到 ScalaCheck 现在开始包含事件的生成来测试一个有状态的系统。但是,我找不到关于它的大量信息。这是否也在 *check 生态系统的其他部分(fscheck、quickcheck、其他变体)中变得流行?
f# - Inconsistent IEnumerable ArgumentException while generating a complex object using FsCheck
The Problem
In F#, I am using FsCheck to generate an object (which I'm then using in an Xunit test, but I can recreate entirely outside of Xunit, so I think we can forget about Xunit). Running the generation 20 times in FSI,
- 50% of the time, the generation runs successfully.
25% of the time, the generation throws:
/li>25% of the time, the generation throws:
/li>
The Situation
The object is as follows:
The object must be follow these rules to be valid:
- All InitEvents must come before all RefEvents
- All InitEvents strings must be unique
- All RefEvent names must have an earlier corresponding InitEvent
- But it's OK if some InitEvents NOT have later corresponding RefEvents
- But it's OK if multiple RefEvents have the same name
The Working Workaround
If I have the generator call a function which returns a valid object and do a Gen.constant (function), I never run into the exceptions, but this is not the way FsCheck is meant to be run! :)
The Broken Right Way?
I cannot seem to completely get away from generating a constant (need to store the list of names outside of the InitEvents so that RefEvent generation can get at them, but I can get more in line with how FsCheck generators seem to work:
What I've Already Checked
- Sorry, forgot to mention in original question that I've tried to Debug in Interactive, and because of the inconsistent behavior, it's somewhat hard to track down. However, when the exceptions hit, it seems to be between the end of my generator code and what is asking for the generated samples -- while FsCheck is DOING the generation, it seems to be trying to process a malformed sequence. I'm further assuming that this is because I've incorrectly coded the generator.
- IndexOutOfRangeException using FsCheck suggests a potentially similar situation. I have tried running my Xunit tests both via Resharper test runner as well as Xunit's console test runner on the real-world tests on which the above simplification is based. Both runners exhibit identical behavior, so the issue is somewhere else.
- Other "How do I generate..." questions such as In FsCheck, how to generate a test record with non-negative fields? and How does one generate a "complex" object in FsCheck? deal with the creation of objects of a lesser complexity. The first was a great help for getting to the code I have, and the second gives a much-needed example of Arb.convert, but Arb.convert doesn't make sense if I'm converting from a "constant" list of randomly generated names. It all seems to come back to that -- the need to make random names, which are then pulled from to make a complete set of InitEvents, and some sequence of RefEvents, both which refer back to the "constant" list, doesn't match anything that I've yet come across.
- I've looked through most examples of FsCheck generators I can find, including the included examples in FsCheck: https://github.com/fscheck/FsCheck/blob/master/examples/FsCheck.Examples/Examples.fs These also do not deal with an object needing internal consistency, and do not seem to apply to this case, even though they have been helpful overall.
- Perhaps this means that I'm approaching the generation of the object from an unhelpful perspective. If there is a different way to generate an object which follows the above rules, I'm open to switching to it.
- Further backing away from the problem, I've seen other SO posts which roughly say "If your object has such restrictions, then what happens when you receive an invalid object? Perhaps you need to rethink the way this object is consumed to better handle invalid cases." If, for example, I were able to initialize on-the-fly a never before seen name in a RefEvent, the entire need for giving an InitEvent first would go away -- the problem gracefully reduces to simply a sequence of RefEvents of some random name. I am open to this kind of solution, but it would require a bit of rework -- in the long run, it may be worth it. In the mean time, the question remains, how can you reliably generate a complex object which follows the above rules using FsCheck?
Thanks!
EDIT(S): Attempts to Solve
The code in Mark Seemann's answer works, but yields a slightly different object than I was looking for (I was unclear in my object rules -- now hopefully clarified). Putting his working code in my generator:
This yields an object where every InitEvent has a matching RefEvent, and there is only one RefEvent for each InitEvent. I'm trying to tweak the code so that I can get multiple RefEvents for each name, and not all names need to have a RefEvent. ex: Init foo, Init bar, Ref foo, Ref foo is perfectly valid. Trying to tweak this with:
The modified code still exhibits the inconsistent behavior. Interestingly, out of 20 sample runs, only three worked (down from 10), while the insufficient number of elements was thrown 8 times and The input must be non-negative was thrown 9 times -- these changes have made the edge case more than twice as likely to be hit. We're now down to a very small section of code with the error.
Mark quickly responded with another version to address changed requirements:
This allowed for some names to not have a RefEvent.
FINAL CODE A very minor tweak gets it so that duplicate RefEvents can happen:
Big thanks to Mark Seemann!