TestBase.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace MyClassesTest
  11. {
  12. public class TestBase
  13. {
  14. public TestContext TestContext { get; set; }
  15. public DataTable TestDataTable { get; set; }
  16. protected string _goodFileName;
  17. protected void SetGoodFileName()
  18. {
  19. _goodFileName = TestContext.Properties["GoodFileName"].ToString();
  20. if (_goodFileName.Contains("[AppPath]"))
  21. {
  22. _goodFileName = _goodFileName.Replace("[AppPath]",
  23. Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
  24. }
  25. }
  26. protected void WriteDescription(Type type)
  27. {
  28. string testName = TestContext.TestName;
  29. // Find the test method currently executing
  30. MemberInfo method = type.GetMethod(testName);
  31. if (method != null)
  32. {
  33. // See if the [Description] attribute exists on this test
  34. Attribute attr = method.GetCustomAttribute(
  35. typeof(DescriptionAttribute));
  36. if (attr != null)
  37. {
  38. // Cast the attribute to a DescriptionAttribute
  39. DescriptionAttribute dattr = (DescriptionAttribute)attr;
  40. // Display the test description
  41. TestContext.WriteLine("Test Description: " + dattr.Description);
  42. }
  43. }
  44. }
  45. public DataTable LoadDataTable(string sql, string connection)
  46. {
  47. TestDataTable = null;
  48. try
  49. {
  50. // Create connection
  51. using (SqlConnection connectionObject = new SqlConnection(connection))
  52. {
  53. // Create command object
  54. using (SqlCommand commandObject = new SqlCommand(sql, connectionObject))
  55. {
  56. // Create a SQL Data Adapter
  57. using (SqlDataAdapter da = new SqlDataAdapter(commandObject))
  58. {
  59. TestDataTable = new DataTable();
  60. da.Fill(TestDataTable);
  61. }
  62. }
  63. }
  64. }
  65. catch (Exception ex)
  66. {
  67. TestContext.WriteLine("Error in LoadDataTable() method." + Environment.NewLine + ex.Message);
  68. return null;
  69. }
  70. return TestDataTable;
  71. }
  72. }
  73. }