1

I am creating a unit test that will test company information into the system but so I can test various assertions in my test.

Here is the code I wrote:

public static Random randnum = new Random();
private string _company = DateTime.Now.ToString("dd/MM/yy ") + ("company") + randnum.Next().ToString();

Basically I am just trying to get a random number to generate with each assertion where the company is listed. So am I writing this wrong or did I forget a piece?

I am using MBUnit.

Please help!

Jonathan
  • 2,318
  • 7
  • 25
  • 44
CSharpDev4Evr
  • 535
  • 1
  • 11
  • 35
  • 1
    What problem are you having with this code? – stuartd Jan 22 '13 at 15:14
  • Not producing a random number with each assertion. I have 8 assertions that run in the test but it gives me the same random number for each one – CSharpDev4Evr Jan 22 '13 at 15:19
  • 1
    Assign _company before each assertion, or use a Get as proposed below. In general, though, random numbers are a bad idea when unit testing as the test can fail intermittently. – stuartd Jan 22 '13 at 15:35
  • @StuartDunkeld I agree! Random numbers unless fully qualified GUIDs are really hard to work with! – Jonathan Jan 22 '13 at 15:36
  • 2
    The use of `Random` and `DateTime.Now` and `Guid.NewGuid()` are Unit Testing Anti Patterns. They make your tests hard to reproduce, while unit tests should be simple to reproduce at all times. http://stackoverflow.com/a/333837/736079 – jessehouwing Jan 22 '13 at 15:40
  • 2/3 for the question lol @jessehouwing – Jonathan Jan 22 '13 at 15:45

2 Answers2

1

Rewrite your unit test to use the following.

public static IEnumerable<object[]> Numbers
    {
        get
        {
            List<object[]> testCases = new List<object[]>();
            Random random = new Random();
            testCases.AddRange(
                (from x in new[]
                {1, 2, 3, 4, 5, 6, 7, 8}
                select new object[] {x})
                .OrderBy(x=>random.Next()));
            return testCases;
        }
    }

    [TestCaseSource("Numbers")]
    public void CreateApplication(
        int number
        )
    {
         string company = DateTime.Now.ToString("dd/MM/yy ") + ("company") + number.ToString();

    }

I suspect your issue is with Immutability (Immutable object). You are setting a private _company field with an initial value. Calls to this value wont regenerate the random number but instead use the value created when this class was instantiated.

Here is a good link which I found interesting regarding random numbers.

How do I seed a random class to avoid getting duplicate random values

Community
  • 1
  • 1
Jonathan
  • 2,318
  • 7
  • 25
  • 44
  • Thanks for your response. Basically I need this random number so when we test those assertions we want to be able to say test this 100 times to make sure we get those correct responses. Now I am able to get random numbers for 4 out of the 8 tests. not sure why though. – CSharpDev4Evr Jan 22 '13 at 16:16
  • Do you want a random number or a number between and including 1 - 8 that generates in a random order? – Jonathan Jan 22 '13 at 16:33
  • @Dev24Amaya what Unit test framework are you using? My solution works with NUnit. – Jonathan Jan 22 '13 at 16:43
  • I am using MB unit, and @Jonathan, I did want a random number that generates in random order for each assertion – CSharpDev4Evr Jan 22 '13 at 17:37
1

I think you can find more about the way Random function works here

The Random class is not a true random number generator. It's a pseudo-random number generator. Any instance of Random has a certain amount of state, and when you call Next (or NextDouble or NextBytes) it will use that state to return you some data which appears to be random, mutating its internal state accordingly so that on the next call you will get another apparently-random number.

lex87
  • 1,276
  • 1
  • 15
  • 33