using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions.Interfaces; using Microsoft.VisualStudio.TestTools.UnitTesting; using MyClasses; using System; using System.IO; namespace MyClassesTest { [TestClass] public class FileProcessTest : TestBase { private const string BAD_FILE_NAME = @"C:\NotExists.bad"; [ClassInitialize()] public static void ClassInitialize(TestContext tc) { // TODO Initialize for all tests in class tc.WriteLine("In ClassInitialize() method"); } [ClassCleanup()] public static void ClassCleanup() { // TODO Cleanup after all test in class } [TestInitialize] public void TestInitialize() { TestContext.WriteLine("In TestInitialize() method"); WriteDescription(this.GetType()); // <-- See TestBase if (TestContext.TestName.StartsWith("FileNameDoesExist")) { SetGoodFileName(); if (!string.IsNullOrEmpty(_goodFileName)) { TestContext.WriteLine("Creating file: " + _goodFileName); // Create the 'Good' file File.AppendAllText(_goodFileName, "Some Text"); } } } [TestCleanup] public void TestCleanup() { TestContext.WriteLine("In TestCleanup() method"); if (TestContext.TestName.StartsWith("FileNameDoesExist")) { // Delete File if (File.Exists(_goodFileName)) { TestContext.WriteLine("Deleting file: " + _goodFileName); File.Delete(_goodFileName); } } } [TestMethod] [DataRow(1, 1, DisplayName = "First Test (1,1)")] [DataRow(42, 42, DisplayName = "Second Test (42,42)")] public void AreNumbersEqual(int num1, int num2) { Assert.AreEqual(num1, num2); } [TestMethod] [DeploymentItem("FileToDeploy.txt")] [DataRow(@"C:\Windows\Regedit.exe", DisplayName = "Regedit.exe")] [DataRow("FileToDeploy.txt", DisplayName = "Deployment Item: FileToDeploy.txt")] public void FileNameUsingDataRow(string fileName) { FileProcess fp = new FileProcess(); bool fromCall; if (!fileName.Contains(@"\")) { fileName = TestContext.DeploymentDirectory + @"\" + fileName; } TestContext.WriteLine("Checking File " + fileName); fromCall = fp.FileExists(fileName); Assert.IsTrue(fromCall); } [TestMethod] [Description("Check to see if a file exists.")] [Owner("Sylvain")] [Priority(1)] [TestCategory("NoException")] //[Ignore] public void FileNameDoesExist() { FileProcess fp = new FileProcess(); bool fromCall; TestContext.WriteLine(@"Checking File " + _goodFileName); fromCall = fp.FileExists(_goodFileName); Assert.IsTrue(fromCall); } //[TestMethod] [Timeout(4000)] public void SimulateTimeout() { System.Threading.Thread.Sleep(200); } [TestMethod] [Description("Check to see if a file does not exists.")] [Owner("Sylvain")] [Priority(1)] [TestCategory("NoException")] public void FileNameDoesNotExist() { FileProcess fp = new FileProcess(); bool fromCall; TestContext.WriteLine(@"Checking File " + BAD_FILE_NAME); fromCall = fp.FileExists(BAD_FILE_NAME); Assert.IsFalse(fromCall); } [TestMethod] [ExpectedException(typeof(ArgumentNullException))] [Description("Check for a throw ArgumentNullException using ExceptedException.")] [Owner("Sylvain")] [Priority(2)] [TestCategory("Exception")] public void FileNameNullOrEmpty_UsingAttribute() { FileProcess fp = new FileProcess(); fp.FileExists(""); } [TestMethod] [Description("Check for a throw ArgumentNullException using try...catch.")] [Owner("Sylvain")] [Priority(2)] [TestCategory("Exception")] public void FileNameNullOrEmpty_UsingTryCatch() { FileProcess fp = new FileProcess(); try { fp.FileExists(""); } catch (ArgumentNullException) { // Test was a success return; } // Fail the test Assert.Fail("Call to FileExists() dit NOT throw an ArgumentNullException."); } } }