数据库连接的 nunit 测试用例

nunit test case for db connection

我是 Nunit 框架、C# 和 dotnet 的新手。如何使用 Nunit 为数据库连接编写测试用例。我找不到任何涵盖数据库连接的 nunit 示例。测试用例必须包括检查连接和关闭它。

namespace TestConnection
{
    class Program
    {
        static void Main(string[] args)
        {
            Connection con = new Connection();
            con.ConnectionString = "Server = ab1-vm.app.test.com ;Port = 6080;Database = MY_DB;Schema = SCHM;User id = admin;PWD = *****;level=3;logname=C:\my_logs";
            Command cmd = con.CreateCommand();
            Encoding e1 = Encoding.Default;
            con.Open();
            cmd.Connection = con;
            try
            {
                string query = "SELECT \"X\", \"Y\", 1 FROM MY_DB..DIM_CONTRACT Z LIMIT 50 ";
                cmd.CommandText = query;
                DataReader rdr = cmd.ExecuteReader();
                while (rdr.HasRows)
                {
                    rdr.Read();
                    System.Console.WriteLine(rdr.GetString(0));
                    System.Console.WriteLine(rdr.GetString(1));
                    System.Console.WriteLine(rdr.GetInt32(2));
                }
                rdr.Close();
                con.Close();
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
            }
        }
    }
}

con.Open()con.close()

调用断言函数 NUnit.Framework.Assert.That(con.State == ConnectionState)
namespace TestProject
{
    [TestClass] //Test Framework
    public class UnitTest1
    {
        Connection con = new Connection();

        [TestMethod] //Test Case 1
        public void TestOpenConnection()
        {
            con.ConnectionString = "Server = ab1-vm.app.test.com ;Port = 6080;Database = MY_DB;Schema = SCHM;User id = admin;PWD = *****;level=3;logname=C:\my_logs";
            con.Open();
            NUnit.Framework.Assert.That(con.State == ConnectionState.Open);
        }

    
        [TestMethod] //Test Case 2
        public void TestCloseConnection()
        {
            con.ConnectionString = "Server = ab1-vm.app.test.com ;Port = 6080;Database = MY_DB;Schema = SCHM;User id = admin;PWD = *****;level=3;logname=C:\my_logs";
            con.Open();  //open the connection
            con.Close(); //close to test if the connection closes
            NUnit.Framework.Assert.That(con.State == ConnectionState.Closed);
        }
    }
}