Expose Random Object as part of the test context

Bug #1111834 reported by Tyrel Alastair Hunter
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
NUnit Framework
Fix Released
Wishlist
Charlie Poole
NUnitLite
Fix Released
Medium
Tyrel Alastair Hunter

Bug Description

When you have a test that needs to generate randomness but the random attribute is not sufficient, there is no way to make sure that the same random number is used across multiple test executions. In order to fix this it would be ideal to expose the already existing Random generator that is part of each test case on the context.

Tags: random

Related branches

Changed in nunit-3.0:
assignee: nobody → Wes Cutting (wesleycutting)
Changed in nunitlite:
assignee: nobody → Wes Cutting (wesleycutting)
Revision history for this message
Charlie Poole (charlie.poole) wrote : Re: [Bug 1111834] Re: Expose Random Object as part of the test context

Take care... you may _not_ want to use the same randomizer that NUnit
uses to create test cases for a method. That randomizer is used at the
time a test is loaded and is re-initialized each time we reload. If
you reuse it at run time, it will have different values each time the
tests are rerun. It may simply be better to create a new randomizer
each time a test is started. For efficiency, you could delay creation
until the test actually accessed that member of the context, so long
as you had the correct seed stored and ready for use.

Charlie

On Fri, Feb 8, 2013 at 12:44 PM, Wes Cutting <email address hidden> wrote:
> ** Changed in: nunit-3.0
> Assignee: (unassigned) => Wes Cutting (wesleycutting)
>
> ** Changed in: nunitlite
> Assignee: (unassigned) => Wes Cutting (wesleycutting)
>
> --
> You received this bug notification because you are subscribed to
> NUnitLite.
> https://bugs.launchpad.net/bugs/1111834
>
> Title:
> Expose Random Object as part of the test context
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/nunit-3.0/+bug/1111834/+subscriptions

Changed in nunit-3.0:
status: New → Triaged
Changed in nunitlite:
status: New → Triaged
importance: Undecided → Wishlist
Changed in nunit-3.0:
importance: Undecided → Wishlist
Revision history for this message
Wes Cutting (wesleycutting) wrote :

We modified the relationship between Randomizer and Random from 'is-a' to 'has-a' in order to delay the construction of the backing Random. This allows us to intercept the seed used to create the Random, and save it into a field which can be queried when writing the results of the test to the XML. All currently used public facing Random methods have had their appropriate wrapper methods added to Randomizer in order to preserve the functional contract; the solution still builds.

Is this an acceptable solution for accessing the seed of the Randomizer? The goal here was to expose the seed so testers can store that value and pass it in for rerunning test identically. If so we will proceed with exposing the Randomizer to the testers in the Test Context.

Revision history for this message
Charlie Poole (charlie.poole) wrote :

That sounds sensible to me. A lot of my classes start out as is-a and
later migrate to has-a for similar reasons.

Charlie

On Wed, Feb 13, 2013 at 2:19 PM, Wes Cutting <email address hidden> wrote:
> We modified the relationship between Randomizer and Random from 'is-a'
> to 'has-a' in order to delay the construction of the backing Random.
> This allows us to intercept the seed used to create the Random, and save
> it into a field which can be queried when writing the results of the
> test to the XML. All currently used public facing Random methods have
> had their appropriate wrapper methods added to Randomizer in order to
> preserve the functional contract; the solution still builds.
>
> Is this an acceptable solution for accessing the seed of the Randomizer?
> The goal here was to expose the seed so testers can store that value and
> pass it in for rerunning test identically. If so we will proceed with
> exposing the Randomizer to the testers in the Test Context.
>
> --
> You received this bug notification because you are subscribed to
> NUnitLite.
> https://bugs.launchpad.net/bugs/1111834
>
> Title:
> Expose Random Object as part of the test context
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/nunit-3.0/+bug/1111834/+subscriptions

Revision history for this message
Wes Cutting (wesleycutting) wrote :

Charlie,

What is the lifecycle of the TestContext? I'm wondering when it is first
created so I know when I can start manipulating it in order to add the
randomizer.

Thanks,
Wes

On Wed, Feb 13, 2013 at 5:10 PM, Charlie Poole <email address hidden> wrote:

> That sounds sensible to me. A lot of my classes start out as is-a and
> later migrate to has-a for similar reasons.
>
> Charlie
>
> On Wed, Feb 13, 2013 at 2:19 PM, Wes Cutting <email address hidden>
> wrote:
> > We modified the relationship between Randomizer and Random from 'is-a'
> > to 'has-a' in order to delay the construction of the backing Random.
> > This allows us to intercept the seed used to create the Random, and save
> > it into a field which can be queried when writing the results of the
> > test to the XML. All currently used public facing Random methods have
> > had their appropriate wrapper methods added to Randomizer in order to
> > preserve the functional contract; the solution still builds.
> >
> > Is this an acceptable solution for accessing the seed of the Randomizer?
> > The goal here was to expose the seed so testers can store that value and
> > pass it in for rerunning test identically. If so we will proceed with
> > exposing the Randomizer to the testers in the Test Context.
> >
> > --
> > You received this bug notification because you are subscribed to
> > NUnitLite.
> > https://bugs.launchpad.net/bugs/1111834
> >
> > Title:
> > Expose Random Object as part of the test context
> >
> > To manage notifications about this bug go to:
> > https://bugs.launchpad.net/nunit-3.0/+bug/1111834/+subscriptions
>
> --
> You received this bug notification because you are a bug assignee.
> https://bugs.launchpad.net/bugs/1111834
>
> Title:
> Expose Random Object as part of the test context
>
> Status in NUnit Test Framework:
> Triaged
> Status in NUnitLite Testing Framework:
> Triaged
>
> Bug description:
> When you have a test that needs to generate randomness but the random
> attribute is not sufficient, there is no way to make sure that the
> same random number is used across multiple test executions. In order
> to fix this it would be ideal to expose the already existing Random
> generator that is part of each test case on the context.
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/nunit-3.0/+bug/1111834/+subscriptions
>

Revision history for this message
Charlie Poole (charlie.poole) wrote :

It lasts only while the test itself is running. I can tell you what
files to look at if you tell me whether you are first working on
nunitlite or nunit-3.0.

Charlie

On Tue, Feb 19, 2013 at 1:14 PM, Wes Cutting <email address hidden> wrote:
> Charlie,
>
> What is the lifecycle of the TestContext? I'm wondering when it is first
> created so I know when I can start manipulating it in order to add the
> randomizer.
>
> Thanks,
> Wes
>
>
> On Wed, Feb 13, 2013 at 5:10 PM, Charlie Poole <email address hidden> wrote:
>
>> That sounds sensible to me. A lot of my classes start out as is-a and
>> later migrate to has-a for similar reasons.
>>
>> Charlie
>>
>> On Wed, Feb 13, 2013 at 2:19 PM, Wes Cutting <email address hidden>
>> wrote:
>> > We modified the relationship between Randomizer and Random from 'is-a'
>> > to 'has-a' in order to delay the construction of the backing Random.
>> > This allows us to intercept the seed used to create the Random, and save
>> > it into a field which can be queried when writing the results of the
>> > test to the XML. All currently used public facing Random methods have
>> > had their appropriate wrapper methods added to Randomizer in order to
>> > preserve the functional contract; the solution still builds.
>> >
>> > Is this an acceptable solution for accessing the seed of the Randomizer?
>> > The goal here was to expose the seed so testers can store that value and
>> > pass it in for rerunning test identically. If so we will proceed with
>> > exposing the Randomizer to the testers in the Test Context.
>> >
>> > --
>> > You received this bug notification because you are subscribed to
>> > NUnitLite.
>> > https://bugs.launchpad.net/bugs/1111834
>> >
>> > Title:
>> > Expose Random Object as part of the test context
>> >
>> > To manage notifications about this bug go to:
>> > https://bugs.launchpad.net/nunit-3.0/+bug/1111834/+subscriptions
>>
>> --
>> You received this bug notification because you are a bug assignee.
>> https://bugs.launchpad.net/bugs/1111834
>>
>> Title:
>> Expose Random Object as part of the test context
>>
>> Status in NUnit Test Framework:
>> Triaged
>> Status in NUnitLite Testing Framework:
>> Triaged
>>
>> Bug description:
>> When you have a test that needs to generate randomness but the random
>> attribute is not sufficient, there is no way to make sure that the
>> same random number is used across multiple test executions. In order
>> to fix this it would be ideal to expose the already existing Random
>> generator that is part of each test case on the context.
>>
>> To manage notifications about this bug go to:
>> https://bugs.launchpad.net/nunit-3.0/+bug/1111834/+subscriptions
>>
>
> --
> You received this bug notification because you are subscribed to
> NUnitLite.
> https://bugs.launchpad.net/bugs/1111834
>
> Title:
> Expose Random Object as part of the test context
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/nunit-3.0/+bug/1111834/+subscriptions

Revision history for this message
Wes Cutting (wesleycutting) wrote :
Download full text (4.0 KiB)

I'm working in nunitlite.

On Tue, Feb 19, 2013 at 2:40 PM, Charlie Poole <email address hidden> wrote:

> It lasts only while the test itself is running. I can tell you what
> files to look at if you tell me whether you are first working on
> nunitlite or nunit-3.0.
>
> Charlie
>
> On Tue, Feb 19, 2013 at 1:14 PM, Wes Cutting <email address hidden>
> wrote:
> > Charlie,
> >
> > What is the lifecycle of the TestContext? I'm wondering when it is first
> > created so I know when I can start manipulating it in order to add the
> > randomizer.
> >
> > Thanks,
> > Wes
> >
> >
> > On Wed, Feb 13, 2013 at 5:10 PM, Charlie Poole <email address hidden>
> wrote:
> >
> >> That sounds sensible to me. A lot of my classes start out as is-a and
> >> later migrate to has-a for similar reasons.
> >>
> >> Charlie
> >>
> >> On Wed, Feb 13, 2013 at 2:19 PM, Wes Cutting <email address hidden>
> >> wrote:
> >> > We modified the relationship between Randomizer and Random from 'is-a'
> >> > to 'has-a' in order to delay the construction of the backing Random.
> >> > This allows us to intercept the seed used to create the Random, and
> save
> >> > it into a field which can be queried when writing the results of the
> >> > test to the XML. All currently used public facing Random methods have
> >> > had their appropriate wrapper methods added to Randomizer in order to
> >> > preserve the functional contract; the solution still builds.
> >> >
> >> > Is this an acceptable solution for accessing the seed of the
> Randomizer?
> >> > The goal here was to expose the seed so testers can store that value
> and
> >> > pass it in for rerunning test identically. If so we will proceed with
> >> > exposing the Randomizer to the testers in the Test Context.
> >> >
> >> > --
> >> > You received this bug notification because you are subscribed to
> >> > NUnitLite.
> >> > https://bugs.launchpad.net/bugs/1111834
> >> >
> >> > Title:
> >> > Expose Random Object as part of the test context
> >> >
> >> > To manage notifications about this bug go to:
> >> > https://bugs.launchpad.net/nunit-3.0/+bug/1111834/+subscriptions
> >>
> >> --
> >> You received this bug notification because you are a bug assignee.
> >> https://bugs.launchpad.net/bugs/1111834
> >>
> >> Title:
> >> Expose Random Object as part of the test context
> >>
> >> Status in NUnit Test Framework:
> >> Triaged
> >> Status in NUnitLite Testing Framework:
> >> Triaged
> >>
> >> Bug description:
> >> When you have a test that needs to generate randomness but the random
> >> attribute is not sufficient, there is no way to make sure that the
> >> same random number is used across multiple test executions. In order
> >> to fix this it would be ideal to expose the already existing Random
> >> generator that is part of each test case on the context.
> >>
> >> To manage notifications about this bug go to:
> >> https://bugs.launchpad.net/nunit-3.0/+bug/1111834/+subscriptions
> >>
> >
> > --
> > You received this bug notification because you are subscribed to
> > NUnitLite.
> > https://bugs.launchpad.net/bugs/1111834
> >
> > Title:
> > Expose Random Object as part of the test context
> >
> > To manage notifications abou...

Read more...

Revision history for this message
Charlie Poole (charlie.poole) wrote :
Download full text (5.2 KiB)

In fact, the TestContext is a temporary object, created each time it
is needed. So storing the randomizer in the TestContext would probably
be inefficient.

I suggest you use a property that directly accesses the cache of
Randomizers, using Randomizer.GetRandomizer(MemberInfo member). You
would have to get the methodinfo for the current test, most-likely
throwing an exception if the property was called from within a setup
method rather than a test, and use that as your argument to
GetRandomizer. A randomizer will be created and cached if it doesn't
already exist.

If this later proves inefficient, the same code could be moved to the
TestExecutionContext, which is referenced by the TestContext, but I
wouldn't be too quick to "optimize" this if I were you.

Charlie

On Tue, Feb 19, 2013 at 2:00 PM, Wes Cutting <email address hidden> wrote:
> I'm working in nunitlite.
>
>
> On Tue, Feb 19, 2013 at 2:40 PM, Charlie Poole <email address hidden> wrote:
>
>> It lasts only while the test itself is running. I can tell you what
>> files to look at if you tell me whether you are first working on
>> nunitlite or nunit-3.0.
>>
>> Charlie
>>
>> On Tue, Feb 19, 2013 at 1:14 PM, Wes Cutting <email address hidden>
>> wrote:
>> > Charlie,
>> >
>> > What is the lifecycle of the TestContext? I'm wondering when it is first
>> > created so I know when I can start manipulating it in order to add the
>> > randomizer.
>> >
>> > Thanks,
>> > Wes
>> >
>> >
>> > On Wed, Feb 13, 2013 at 5:10 PM, Charlie Poole <email address hidden>
>> wrote:
>> >
>> >> That sounds sensible to me. A lot of my classes start out as is-a and
>> >> later migrate to has-a for similar reasons.
>> >>
>> >> Charlie
>> >>
>> >> On Wed, Feb 13, 2013 at 2:19 PM, Wes Cutting <email address hidden>
>> >> wrote:
>> >> > We modified the relationship between Randomizer and Random from 'is-a'
>> >> > to 'has-a' in order to delay the construction of the backing Random.
>> >> > This allows us to intercept the seed used to create the Random, and
>> save
>> >> > it into a field which can be queried when writing the results of the
>> >> > test to the XML. All currently used public facing Random methods have
>> >> > had their appropriate wrapper methods added to Randomizer in order to
>> >> > preserve the functional contract; the solution still builds.
>> >> >
>> >> > Is this an acceptable solution for accessing the seed of the
>> Randomizer?
>> >> > The goal here was to expose the seed so testers can store that value
>> and
>> >> > pass it in for rerunning test identically. If so we will proceed with
>> >> > exposing the Randomizer to the testers in the Test Context.
>> >> >
>> >> > --
>> >> > You received this bug notification because you are subscribed to
>> >> > NUnitLite.
>> >> > https://bugs.launchpad.net/bugs/1111834
>> >> >
>> >> > Title:
>> >> > Expose Random Object as part of the test context
>> >> >
>> >> > To manage notifications about this bug go to:
>> >> > https://bugs.launchpad.net/nunit-3.0/+bug/1111834/+subscriptions
>> >>
>> >> --
>> >> You received this bug notification because you are a bug assignee.
>> >> https://bugs.launchpad.net/bugs/1111834
>> >>
>> >> Title:
>> >> Expose Rand...

Read more...

Changed in nunit-3.0:
assignee: Wes Cutting (wesleycutting) → nobody
Revision history for this message
Charlie Poole (charlie.poole) wrote :

Why the change to nobody working on it?

Revision history for this message
Tyrel Alastair Hunter (ialastairhunter) wrote :

I have a couple interns that are going to be working for me and my team for 10 weeks. I want them to focus on making the changes to nunitlite which is what we need for our needs. Then if they have time we will have them merge the changes into nunit 3.0. If not I will merge the changes in myself.

Revision history for this message
Charlie Poole (charlie.poole) wrote :

That sounds great. Make sure that they work with the latest code and merge
changes into their own code as they go - at least if any other changes are
made.

Charlie

On Tue, Apr 9, 2013 at 9:53 AM, Tyrel Alastair Hunter <
<email address hidden>> wrote:

> I have a couple interns that are going to be working for me and my team
> for 10 weeks. I want them to focus on making the changes to nunitlite
> which is what we need for our needs. Then if they have time we will have
> them merge the changes into nunit 3.0. If not I will merge the changes
> in myself.
>
> --
> You received this bug notification because you are subscribed to
> NUnitLite.
> https://bugs.launchpad.net/bugs/1111834
>
> Title:
> Expose Random Object as part of the test context
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/nunit-3.0/+bug/1111834/+subscriptions
>

Changed in nunitlite:
assignee: Wes Cutting (wesleycutting) → Tyrel Alastair Hunter (ialastairhunter)
Changed in nunit-3.0:
assignee: nobody → Tyrel Alastair Hunter (ialastairhunter)
Revision history for this message
Tyrel Alastair Hunter (ialastairhunter) wrote :

I have added the proposed interface for the random generator to the issue as an attatchment

Revision history for this message
Tyrel Alastair Hunter (ialastairhunter) wrote :

This is the proposed interface for the random generator that will be exposed to during test execution via the test context.

see nunit-discuss for the discussion on this interface

https://groups.google.com/forum/?fromgroups=#!topic/nunit-discuss/-OkpTnOeIRQ

Revision history for this message
Charlie Poole (charlie.poole) wrote :

The interface looks good to me. BTW, I'm assuming we are using the word
"interface" in a general way - the methods can just as well be directly
implemented on the type that is returned by TestContext.Random.

Charlie

On Mon, Apr 15, 2013 at 12:55 PM, Tyrel Alastair Hunter <
<email address hidden>> wrote:

> This is the proposed interface for the random generator that will be
> exposed to during test execution via the test context.
>
> see nunit-discuss for the discussion on this interface
>
> https://groups.google.com/forum/?fromgroups=#!topic/nunit-
> discuss/-OkpTnOeIRQ
>
>
> ** Attachment added: "proposed interface"
>
> https://bugs.launchpad.net/nunitlite/+bug/1111834/+attachment/3644956/+files/RandomGenerator.png
>
> --
> You received this bug notification because you are subscribed to
> NUnitLite.
> https://bugs.launchpad.net/bugs/1111834
>
> Title:
> Expose Random Object as part of the test context
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/nunit-3.0/+bug/1111834/+subscriptions
>

Revision history for this message
Tyrel Alastair Hunter (ialastairhunter) wrote :

You assumption is correct I should have used class definition instead of interface sorry for any confusion

Sent from Mailbox for iPhone

On Mon, Apr 15, 2013 at 4:32 PM, Charlie Poole <email address hidden> wrote:

> The interface looks good to me. BTW, I'm assuming we are using the word
> "interface" in a general way - the methods can just as well be directly
> implemented on the type that is returned by TestContext.Random.
> Charlie
> On Mon, Apr 15, 2013 at 12:55 PM, Tyrel Alastair Hunter <
> <email address hidden>> wrote:
>> This is the proposed interface for the random generator that will be
>> exposed to during test execution via the test context.
>>
>> see nunit-discuss for the discussion on this interface
>>
>> https://groups.google.com/forum/?fromgroups=#!topic/nunit-
>> discuss/-OkpTnOeIRQ
>>
>>
>> ** Attachment added: "proposed interface"
>>
>> https://bugs.launchpad.net/nunitlite/+bug/1111834/+attachment/3644956/+files/RandomGenerator.png
>>
>> --
>> You received this bug notification because you are subscribed to
>> NUnitLite.
>> https://bugs.launchpad.net/bugs/1111834
>>
>> Title:
>> Expose Random Object as part of the test context
>>
>> To manage notifications about this bug go to:
>> https://bugs.launchpad.net/nunit-3.0/+bug/1111834/+subscriptions
>>
> --
> You received this bug notification because you are subscribed to the bug
> report.
> https://bugs.launchpad.net/bugs/1111834
> Title:
> Expose Random Object as part of the test context
> Status in NUnit Test Framework:
> Triaged
> Status in NUnitLite Testing Framework:
> Triaged
> Bug description:
> When you have a test that needs to generate randomness but the random
> attribute is not sufficient, there is no way to make sure that the
> same random number is used across multiple test executions. In order
> to fix this it would be ideal to expose the already existing Random
> generator that is part of each test case on the context.
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/nunit-3.0/+bug/1111834/+subscriptions

Revision history for this message
Charlie Poole (charlie.poole) wrote :

No confusion. I actually started out using the word. It's just another
overload.

Charlie

On Mon, Apr 15, 2013 at 4:05 PM, Tyrel Alastair Hunter <
<email address hidden>> wrote:

> You assumption is correct I should have used class definition instead of
> interface sorry for any confusion
> —
> Sent from Mailbox for iPhone
>
> On Mon, Apr 15, 2013 at 4:32 PM, Charlie Poole <email address hidden>
> wrote:
>
> > The interface looks good to me. BTW, I'm assuming we are using the word
> > "interface" in a general way - the methods can just as well be directly
> > implemented on the type that is returned by TestContext.Random.
> > Charlie
> > On Mon, Apr 15, 2013 at 12:55 PM, Tyrel Alastair Hunter <
> > <email address hidden>> wrote:
> >> This is the proposed interface for the random generator that will be
> >> exposed to during test execution via the test context.
> >>
> >> see nunit-discuss for the discussion on this interface
> >>
> >> https://groups.google.com/forum/?fromgroups=#!topic/nunit-
> >> discuss/-OkpTnOeIRQ
> >>
> >>
> >> ** Attachment added: "proposed interface"
> >>
> >>
> https://bugs.launchpad.net/nunitlite/+bug/1111834/+attachment/3644956/+files/RandomGenerator.png
> >>
> >> --
> >> You received this bug notification because you are subscribed to
> >> NUnitLite.
> >> https://bugs.launchpad.net/bugs/1111834
> >>
> >> Title:
> >> Expose Random Object as part of the test context
> >>
> >> To manage notifications about this bug go to:
> >> https://bugs.launchpad.net/nunit-3.0/+bug/1111834/+subscriptions
> >>
> > --
> > You received this bug notification because you are subscribed to the bug
> > report.
> > https://bugs.launchpad.net/bugs/1111834
> > Title:
> > Expose Random Object as part of the test context
> > Status in NUnit Test Framework:
> > Triaged
> > Status in NUnitLite Testing Framework:
> > Triaged
> > Bug description:
> > When you have a test that needs to generate randomness but the random
> > attribute is not sufficient, there is no way to make sure that the
> > same random number is used across multiple test executions. In order
> > to fix this it would be ideal to expose the already existing Random
> > generator that is part of each test case on the context.
> > To manage notifications about this bug go to:
> > https://bugs.launchpad.net/nunit-3.0/+bug/1111834/+subscriptions
>
> --
> You received this bug notification because you are subscribed to
> NUnitLite.
> https://bugs.launchpad.net/bugs/1111834
>
> Title:
> Expose Random Object as part of the test context
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/nunit-3.0/+bug/1111834/+subscriptions
>

Changed in nunit-3.0:
status: Triaged → In Progress
Changed in nunitlite:
status: Triaged → In Progress
Changed in nunitlite:
importance: Wishlist → Medium
milestone: none → 0.9
Changed in nunitlite:
status: In Progress → Fix Committed
Changed in nunitlite:
status: Fix Committed → Fix Released
Changed in nunit-3.0:
assignee: Tyrel Alastair Hunter (ialastairhunter) → Charlie Poole (charlie.poole)
milestone: none → 2.9.6
status: In Progress → Fix Committed
Changed in nunit-3.0:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.