Advertisement
sylviapsh

Unit Test - Console.Set In/Out

May 19th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.57 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5.  
  6. [TestClass]
  7. public class UnitTestPerformance
  8. {
  9.     [TestMethod]
  10.     [Timeout(5000)]
  11.     public void TestSearchPerformance()
  12.     {
  13.         int addsCount = 10000;
  14.         int searchCount = 50000;
  15.  
  16.         // Prepare the input commands
  17.         StringBuilder input = new StringBuilder();
  18.         for (int i = 0; i < addsCount; i++)
  19.         {
  20.             input.AppendLine("Add application: app; a; 12345; http://oldurl");
  21.         }
  22.         for (int i = 0; i < searchCount; i++)
  23.         {
  24.             input.AppendLine("Find: app; 100");
  25.         }
  26.         input.AppendLine("End");
  27.  
  28.         // Prepare the expected result
  29.         StringBuilder expectedOutput = new StringBuilder();
  30.         for (int i = 0; i < addsCount; i++)
  31.         {
  32.             expectedOutput.AppendLine("Application added");
  33.         }
  34.         for (int i = 0; i < searchCount * 100; i++)
  35.         {
  36.             expectedOutput.AppendLine("Application: app; a; 12345; http://oldurl");
  37.         }
  38.  
  39.         // Redirect the console input / output and invoke the Main() method
  40.         Console.SetIn(new StringReader(input.ToString()));
  41.         StringWriter consoleOutput = new StringWriter();
  42.         Console.SetOut(consoleOutput);
  43.         Problem04_Free_Content.Program.Main();
  44.  
  45.         // Assert that the program execution result is correct
  46.         string expected = expectedOutput.ToString();
  47.         string actual = consoleOutput.ToString();
  48.         Assert.AreEqual(expected, actual);
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement