| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- 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.");
- }
- }
- }
|