If you are fixing the seed, you are probably over constraining your test. 
 Even though you are calling rand, there must be some post-conditions that 
the function is supposed to guarantee.  In this case, you probably expect 
all options to be returned with equal probability, so you should call the 
function many times, and calculate those probabilities.  There is a whole 
field for statistical testing.


On Tuesday, 14 March 2017 23:45:22 UTC-4, Doug Ireton wrote:
>
> I'm a new Gopher and I'm working through "Learn Go" by Nathan Youngman 
> and trying to TDD the exercises to learn how to write testable Go code.
>
> I have a function to return a random spaceline 
> <https://play.golang.org/p/g5JnrIFyjo> from a string array.
>
> In Go, how do I test functions which depend on random numbers? And, yes, I 
> know that "math/rand" isn't truly random.
>
> Is it as simple as setting a seed right before I run my test, e.g. 
> rand.Seed(1)? Do I set rand.Seed(1) at the top of the _test.go file, or 
> at the beginning of each unit test?
>
> Also, am I seeding math.rand correctly in the Init() function? Will 
> seeding it in the Init() function override any seeding I do in my tests?
>
> My only other thought is to create an interface somehow to mock 
> rand.Intn(), but this seems like overkill and I don't know enough about 
> interfaces to know if this is inadvisable.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to