我是 C# 单元测试领域的新手。
我有一段代码在我的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;
}
现在我不知道如何用单元测试来测试这段代码有人能给我一个例子吗?
编辑:
我做了一个测试代码
[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");
}
但它给了我以下错误:消息:测试'RenderingPasswordShouldHaveMaximumSize'超出了执行超时期限,有人可以帮我解决这个问题,最大尺寸为74!