Home > Software design >  How to increment an int every time I run my tests
How to increment an int every time I run my tests

Time:10-28

I'm testing my methods of one database but I have a problem with my Insert test because my object has an autoincrement ID which is created by default when insert the object BUT also it has a name which has to be unique, so the first time I run my test it works perfectly but no more. I though about put an int i = 0 and then increment it and interpolate it into my string but it is reset every time I run my test so it doesn't work. Any ideas?

This is my test:

public async Task CanCreateStringSignal()
{
    var s = new SignalsDTO()
    {
        Name = "Signal",
        TagType = "String",
        Description = "Foo"
    };
    
    var idReturn = await _handler.Create(s);
    var signal = await _handler.GetSignalsByName("Signal");

    Assert.IsNotNull(signal);
    Assert.IsInstanceOfType(signal, typeof(SignalsDTO));
    Assert.AreEqual("Signal", signal.Name);
    Assert.AreEqual(idReturn, signal.IDTag);
}

CodePudding user response:

Just append some GUID and you'll be fine:

Name = "Signal"   Guid.NewGuid()

Now, on each run, now and in 100 years, the name will be unique.

  • Related