使用 Kurt Schelfthout 的答案作为基础,您可以为video
像这样的类:
public static class VideoArbitrary
{
public static Arbitrary<Video> Videos()
{
var genVideo = from w in Arb.Generate<PositiveInt>()
from h in Arb.Generate<PositiveInt>()
from arrs in Gen.ListOf(
Gen.Array2DOf<int>(
h.Item,
w.Item,
Arb.Generate<int>()))
select new Video(w.Item, h.Item, arrs);
return genVideo.ToArbitrary();
}
}
您可以通过多种方式使用它。
纯香草 FsCheck
以下是如何将 Video Arbitrary 与普通的 FsCheck 一起使用,这里托管在 xUnit.net 测试用例中,这不是必需的:您可以在您喜欢的任何进程中托管它:
[Fact]
public void VideoProperty()
{
var property = Prop.ForAll(
VideoArbitrary.Videos(),
video =>
{
// Test goes here...
Assert.NotNull(video);
});
property.QuickCheckThrowOnFailure();
}
Prop.ForAll
对于使用自定义任意值定义属性非常有用。你打电话时QuickCheckThrowOnFailure
,它将针对“所有”(默认情况下:100)值运行测试Video
class.
无类型 xUnit.net 属性
您还可以使用 FsCheck.Xunit Glue 库,但必须将 Arbitrary 作为弱类型值传递给属性:
[Property(Arbitrary = new[] { typeof(VideoArbitrary) })]
public void XunitPropertyWithWeaklyTypedArbitrary(Video video)
{
// Test goes here...
Assert.NotNull(video);
}
这很简单且易于理解,但是在分配该值时不涉及静态类型检查Arbitrary
财产,所以我不太喜欢这种方法。
输入 xUnit.net 属性
将 FsCheck.Xunit 与自定义任意波形一起使用的更好方法是将其与 Prop.ForAll 结合起来 http://blog.ploeh.dk/2015/09/08/ad-hoc-arbitraries-with-fscheckxunit:
[Property]
public Property XUnitPropertyWithStronglyTypedArbitrary()
{
return Prop.ForAll(
VideoArbitrary.Videos(),
video =>
{
// Test goes here...
Assert.NotNull(video);
});
}
请注意,该方法的返回类型不再是void
, but Property
; the [Property]
属性理解这种类型并相应地执行测试。
第三个选项是我在 xUnit.net 中使用自定义任意值的首选方法,因为它会带回编译时检查。