Maarten Verstraten
Maarten Verstraten

Reputation: 159

Unit testing in C# how to?

I am new in the world of unit testing in C#.

I have a piece of code in my Main.cs

public static string Generate(int length)
{
    char[] chars = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&".ToCharArray();
    string password = string.Empty;
    Random random = new Random();

    for (int i = 0; i < length; i++)
    {
        int x = random.Next(1, chars.Length);

        if (!password.Contains(chars.GetValue(x).ToString()))
            password += chars.GetValue(x);
        else
            i--;
    }
    return password;
}

now i don't know how to test this code with unit tests can someone give me a example?

EDIT:

I have make a test code

    [TestMethod]
    [Timeout(1000)]
    public void RenderingPasswordShouldHaveMaximumSize()
    {
        var amountOfCharacters = Int32.MaxValue;
        var generator = new PasswordGenerator();
        var target = generator.Generate(amountOfCharacters);

        // TODO
        Assert.Fail("This method should throw an exception if you try to create a password with too many characters");
    }

But it gives me the following error: Message: Test 'RenderingPasswordShouldHaveMaximumSize' exceeded execution timeout period can someone help me with this needs to have a max size of 74!

Upvotes: 0

Views: 5795

Answers (4)

Phill
Phill

Reputation: 18804

var amountOfCharacters = Int32.MaxValue;
var generator = new PasswordGenerator();
var target = generator.Generate(amountOfCharacters);

You're specifying the number of characters the password should contain as 2,147,483,647 characters...

chars.Length

There's only 74 possible values in your array.

No doubt it times out because the loop takes longer to iterate 2.2 billion times trying to find the last few values in your array.

for (int i = 0; i < length; i++)

Your logic changes too since you're not specifying the length of the password, but the number of iterations you want to do.

Upvotes: 0

Volodymyr Machula
Volodymyr Machula

Reputation: 1604

There are much nuances in unit testing. I would recommend you to read book about unit testing "The Art of Unit Testing: With Examples in .Net".

There are described a lot of techniques and approach in unit testing. You can also find many examples here.

Upvotes: 1

Oscar Mederos
Oscar Mederos

Reputation: 29863

A simple example using NUnit. Here I'm testing that when I pass 0 as an argument, nothing is generated (perhaps you should throw an Exception?)

[TextFixture]
public class Tests {

    [Test]
    public void Test_Length0_ReturnsNothing() {
        string result = Generate(0);

        Assert.IsTrue(string.IsNullOrEmpty(result));
    }
}

You can write then similar tests (eg. to make sure it include the characters you want, etc).

Upvotes: 1

Boas Enkler
Boas Enkler

Reputation: 12557

the idea of unit testing is to put something in a small method of you and check if the result is ok.

Visual Studio there is a project template for that. and there are also other tools like NUnit oderXUnit for tests in C#

There is a great pluralsight course:

and a webcast of Uncle Bob in which he demonstrates test driven development http://cleancoders.com/codecast/clean-code-episode-6-part-1/show

See also "Verifying Code by using Unit Tests" at msdn

Upvotes: 1

Related Questions