fscheck / FsCheck

Random Testing for .NET
https://fscheck.github.io/FsCheck/
BSD 3-Clause "New" or "Revised" License
1.15k stars 154 forks source link

How to globally register arbitraries in 3.x #632

Closed spingee closed 1 year ago

spingee commented 1 year ago

Hello is there a way how to globally register arbitraries (defined in static class with static methods returning arbitrary) like it was with Arb.Register (i currently use C#). I used to call this static method in xunit constructor and arbitraries where available for all tests. Currently ive only found a way to put it to property attribute on every test [Property(Arbitrary = new []{typeof(MyGenerators)})]. Thanks for answer.

bartelink commented 1 year ago

you can do assembly: Properties Examples: https://github.com/search?q=repo%3Ajet%2Fdotnet-templates%20%22%5B%3Cassembly%3A%20Properties(%20Arbitrary%20%3D%22&type=code

spingee commented 1 year ago

There is really no way how to do it per test class?

bartelink commented 1 year ago

if you are making a test class (type, then I believe you should be able to tag that type with Properties Something like

[<Properties(Arbitraries= [| typeof<Generators>) |]>]
type MyProps() =
  let p1 = ...
  member _.p2 = ...  

p2 and p2 should inherit that default arbs

(I tend to try to use <PackageReference Include="FsCheck.xUnit" Version="3.0.0-beta2" />, but some of github/jet is still on V2)

Another technique that can work well is to have a local PropertiesAttribute and/or other one that defines a 'profile', e.g.: https://github.com/jet/equinox/blob/c7c4f62048e038395bbda0768231dd3f0bf5ff14/tests/Equinox.MemoryStore.Integration/MemoryStoreIntegration.fs#L8-L14

The single best resource IME, in case you have not happened to see it is probably: https://paul.blasuc.ci/index.html

The docs are pretty all over the place on all this, sadly - if you have the time, adding some self-answered stack overflow questions might eb a good way to document this knowledge (my contribution to the world will, in due course, be for github/jet to all use FsCheck V34 and use consistent style!)

Wait, found 'the documentation' in which I found it before: https://github.com/fscheck/FsCheck/blob/master/tests/FsCheck.Test/Fscheck.XUnit/PropertyAttributeTests.fs (Was not aware tests in a module could work that way too)

spingee commented 1 year ago

Properties attribute on test class works, thank you very much