FileProcessTest.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions.Interfaces;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using MyClasses;
  4. using System;
  5. using System.IO;
  6. namespace MyClassesTest
  7. {
  8. [TestClass]
  9. public class FileProcessTest : TestBase
  10. {
  11. private const string BAD_FILE_NAME = @"C:\NotExists.bad";
  12. [ClassInitialize()]
  13. public static void ClassInitialize(TestContext tc)
  14. {
  15. // TODO Initialize for all tests in class
  16. tc.WriteLine("In ClassInitialize() method");
  17. }
  18. [ClassCleanup()]
  19. public static void ClassCleanup()
  20. {
  21. // TODO Cleanup after all test in class
  22. }
  23. [TestInitialize]
  24. public void TestInitialize()
  25. {
  26. TestContext.WriteLine("In TestInitialize() method");
  27. WriteDescription(this.GetType()); // <-- See TestBase
  28. if (TestContext.TestName.StartsWith("FileNameDoesExist"))
  29. {
  30. SetGoodFileName();
  31. if (!string.IsNullOrEmpty(_goodFileName))
  32. {
  33. TestContext.WriteLine("Creating file: " + _goodFileName);
  34. // Create the 'Good' file
  35. File.AppendAllText(_goodFileName, "Some Text");
  36. }
  37. }
  38. }
  39. [TestCleanup]
  40. public void TestCleanup()
  41. {
  42. TestContext.WriteLine("In TestCleanup() method");
  43. if (TestContext.TestName.StartsWith("FileNameDoesExist"))
  44. {
  45. // Delete File
  46. if (File.Exists(_goodFileName))
  47. {
  48. TestContext.WriteLine("Deleting file: " + _goodFileName);
  49. File.Delete(_goodFileName);
  50. }
  51. }
  52. }
  53. [TestMethod]
  54. [DataRow(1, 1, DisplayName = "First Test (1,1)")]
  55. [DataRow(42, 42, DisplayName = "Second Test (42,42)")]
  56. public void AreNumbersEqual(int num1, int num2)
  57. {
  58. Assert.AreEqual(num1, num2);
  59. }
  60. [TestMethod]
  61. [DeploymentItem("FileToDeploy.txt")]
  62. [DataRow(@"C:\Windows\Regedit.exe", DisplayName = "Regedit.exe")]
  63. [DataRow("FileToDeploy.txt", DisplayName = "Deployment Item: FileToDeploy.txt")]
  64. public void FileNameUsingDataRow(string fileName)
  65. {
  66. FileProcess fp = new FileProcess();
  67. bool fromCall;
  68. if (!fileName.Contains(@"\"))
  69. {
  70. fileName = TestContext.DeploymentDirectory + @"\" + fileName;
  71. }
  72. TestContext.WriteLine("Checking File " + fileName);
  73. fromCall = fp.FileExists(fileName);
  74. Assert.IsTrue(fromCall);
  75. }
  76. [TestMethod]
  77. [Description("Check to see if a file exists.")]
  78. [Owner("Sylvain")]
  79. [Priority(1)]
  80. [TestCategory("NoException")]
  81. //[Ignore]
  82. public void FileNameDoesExist()
  83. {
  84. FileProcess fp = new FileProcess();
  85. bool fromCall;
  86. TestContext.WriteLine(@"Checking File " + _goodFileName);
  87. fromCall = fp.FileExists(_goodFileName);
  88. Assert.IsTrue(fromCall);
  89. }
  90. //[TestMethod]
  91. [Timeout(4000)]
  92. public void SimulateTimeout()
  93. {
  94. System.Threading.Thread.Sleep(200);
  95. }
  96. [TestMethod]
  97. [Description("Check to see if a file does not exists.")]
  98. [Owner("Sylvain")]
  99. [Priority(1)]
  100. [TestCategory("NoException")]
  101. public void FileNameDoesNotExist()
  102. {
  103. FileProcess fp = new FileProcess();
  104. bool fromCall;
  105. TestContext.WriteLine(@"Checking File " + BAD_FILE_NAME);
  106. fromCall = fp.FileExists(BAD_FILE_NAME);
  107. Assert.IsFalse(fromCall);
  108. }
  109. [TestMethod]
  110. [ExpectedException(typeof(ArgumentNullException))]
  111. [Description("Check for a throw ArgumentNullException using ExceptedException.")]
  112. [Owner("Sylvain")]
  113. [Priority(2)]
  114. [TestCategory("Exception")]
  115. public void FileNameNullOrEmpty_UsingAttribute()
  116. {
  117. FileProcess fp = new FileProcess();
  118. fp.FileExists("");
  119. }
  120. [TestMethod]
  121. [Description("Check for a throw ArgumentNullException using try...catch.")]
  122. [Owner("Sylvain")]
  123. [Priority(2)]
  124. [TestCategory("Exception")]
  125. public void FileNameNullOrEmpty_UsingTryCatch()
  126. {
  127. FileProcess fp = new FileProcess();
  128. try
  129. {
  130. fp.FileExists("");
  131. }
  132. catch (ArgumentNullException)
  133. {
  134. // Test was a success
  135. return;
  136. }
  137. // Fail the test
  138. Assert.Fail("Call to FileExists() dit NOT throw an ArgumentNullException.");
  139. }
  140. }
  141. }