Reputation: 9
I have created a Hello World program and I am new to C#, My program will print 100 words like that as follow
public static void Main(/*I forgot arguments*/)
{
string []s=new string [100];
foreach(string ss in s)
{
ss="Hello World";
Console.WriteLine("{0}\n",ss);
}
}
Could you show me step-by-step how to create a test for this program ? Does it need one ? I don't have an image of how testers do the test. Sorry I am stupid.
I think I have tried my best, no one ever find I find it unworthy to me not to get any help ? I don't need the class because I forgot the class long ago after the accident.
Upvotes: 0
Views: 97
Reputation: 95
First of all, you can't (or - you shouldn't) test void
methods. You are testing the output of the method - which Main does not have. Second thought: you cannot mock (simulate) an Console
object. Read some tutorials about mock and mocking.
Sample method with sample test could look similar to this:
public class SimpleCalculator
{
public int SumTwoNumbers(int number1, int number2)
{
return number1 + number2;
}
}
[TestClass]
public class TestClass
{
[TestMethod]
public void Test_SimpleCalculator_SumTwoNumbers_CorrectValues()
{
// Arrange
SimpleCalculator calc = new SimpleCalculator();
// Act
int result = calc.SumTwoNumbers(5, 2);
// Assert
Assert.AreEqual(7, result);
}
}
Hope this helped a little.
Upvotes: 1
Reputation: 25465
Why would you need a test for this? What would you be testing for? Instead of how to test the code I'd be looking at how you can make it better.
Have a look at the Main below.
public static void Main(/*I forgot arguments*/)
{
for(var i = 0; i < 100; i++)
{
Console.WriteLine("Hello World");
}
}
Note, you don't use the array you create so there is no need to create it. Use a for loop when you know exactly how many times you need to loop. Also, there is no to format your string if you are not concating it with other strings.
Upvotes: 1
Reputation: 1919
Flush the above code to a method than having it in the main. Then refer to some unit test cases documents (If unit testing is what your looking for) here http://www.nunit.org/index.php?p=quickStart&r=2.5.10
Upvotes: 0