| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- namespace MyClassesTest
- {
- public class TestBase
- {
- public TestContext TestContext { get; set; }
- public DataTable TestDataTable { get; set; }
- protected string _goodFileName;
- protected void SetGoodFileName()
- {
- _goodFileName = TestContext.Properties["GoodFileName"].ToString();
- if (_goodFileName.Contains("[AppPath]"))
- {
- _goodFileName = _goodFileName.Replace("[AppPath]",
- Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
- }
- }
- protected void WriteDescription(Type type)
- {
- string testName = TestContext.TestName;
- // Find the test method currently executing
- MemberInfo method = type.GetMethod(testName);
- if (method != null)
- {
- // See if the [Description] attribute exists on this test
- Attribute attr = method.GetCustomAttribute(
- typeof(DescriptionAttribute));
- if (attr != null)
- {
- // Cast the attribute to a DescriptionAttribute
- DescriptionAttribute dattr = (DescriptionAttribute)attr;
- // Display the test description
- TestContext.WriteLine("Test Description: " + dattr.Description);
- }
- }
- }
- public DataTable LoadDataTable(string sql, string connection)
- {
- TestDataTable = null;
- try
- {
- // Create connection
- using (SqlConnection connectionObject = new SqlConnection(connection))
- {
- // Create command object
- using (SqlCommand commandObject = new SqlCommand(sql, connectionObject))
- {
- // Create a SQL Data Adapter
- using (SqlDataAdapter da = new SqlDataAdapter(commandObject))
- {
- TestDataTable = new DataTable();
- da.Fill(TestDataTable);
- }
- }
- }
- }
- catch (Exception ex)
- {
- TestContext.WriteLine("Error in LoadDataTable() method." + Environment.NewLine + ex.Message);
- return null;
- }
- return TestDataTable;
- }
- }
- }
|