You could also capture a (p)random stream as a test data vector and use it
in lieu of the PRNG during tests. This is the same as seeding iff and only
iff the PRNG is never changed, Should that change, then so will the tests
unless you capture a test vector now. (For example, if you have a timing
benchmark and the amount of work varies, then having a a cache of static
random values makes the timing always be comparable.)

This is pedantic and probably not important to most people. But if it
matters to you, it is the answer.

On Wed, Mar 15, 2017 at 11:25 PM, <prades.m...@gmail.com> wrote:

> You would test it the same way you test anything that depends on IO, you
> don't make your function depend on a random number or an unpredictable
> value, the random number should be a function parameter.
>
> In order to test something it must be made testable. Right now your
> function is not.
>
>
> Le mercredi 15 mars 2017 04:45:22 UTC+1, Doug Ireton a écrit :
>>
>> 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.
>



-- 
Michael T. Jones
michael.jo...@gmail.com

-- 
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