FileProcessDataDriven.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using MyClasses;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace MyClassesTest
  10. {
  11. [TestClass]
  12. public class FileProcessDataDriven : TestBase
  13. {
  14. [TestMethod]
  15. public void FileExistsTestFromDB()
  16. {
  17. FileProcess fp = new FileProcess();
  18. bool fromCall = false;
  19. bool testFailed = false;
  20. string fileName;
  21. bool expectedValue;
  22. bool causesException;
  23. string sql = "SELECT * FROM tests.FileProcessTest";
  24. string conn = TestContext.Properties["ConnectionString"].ToString();
  25. // Load data from SQL server table
  26. TestDataTable = LoadDataTable(sql, conn);
  27. if (TestDataTable != null)
  28. {
  29. // Loop throught all row in table
  30. foreach (DataRow row in TestDataTable.Rows)
  31. {
  32. // Get values from data row
  33. fileName = row["FileName"].ToString();
  34. expectedValue = Convert.ToBoolean(row["ExpectedValue"]);
  35. causesException = Convert.ToBoolean(row["CausesException"]);
  36. try
  37. {
  38. //See if file exists
  39. fromCall = fp.FileExists(fileName);
  40. }
  41. catch (ArgumentNullException)
  42. {
  43. // See if a null value was expected
  44. if (!causesException)
  45. {
  46. testFailed = true;
  47. }
  48. }
  49. catch (Exception)
  50. {
  51. testFailed = true;
  52. }
  53. TestContext.WriteLine("Testing File: '{0}', Expected Value: '{1}', Actual Value: '{2}', Result: '{3}'", fileName, expectedValue, fromCall, (expectedValue == fromCall ? "Success" : "FAILED"));
  54. // Check assertion
  55. if (expectedValue != fromCall)
  56. {
  57. testFailed = true;
  58. }
  59. }
  60. if (testFailed)
  61. {
  62. Assert.Fail("Data Driven Tests Have Failed, Check Additional Output for More Information.");
  63. }
  64. }
  65. else
  66. {
  67. Assert.Fail("Something in the connection to DB.");
  68. }
  69. }
  70. }
  71. }