0

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!

6
  • 2
    possible duplicate of stackoverflow.com/questions/2101/… Commented Feb 21, 2013 at 8:29
  • What do you want to test for? The only bit that can fail is when length > chars.Length. Commented Feb 21, 2013 at 8:29
  • 3
    Why can a specific character only be added once? By doing so you just make the password less secure. And you might want to check for a minimum length Commented Feb 21, 2013 at 8:30
  • you could start with creating a unit test that tests a new class that is responsible for generating a password. Commented Feb 21, 2013 at 8:44
  • -1 editing your question to read "tyyyyyyyyyyyyyyyyyydjythtthththttht" is not helpful. Commented Feb 21, 2013 at 11:05

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

null would surely not be a valid passing test case.
@leppie I focused my answer on how to write the test. That was the first thing that came to my mind.
1

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.