SetUp不适用于NUnit

时间:2012-11-05 10:39:36

标签: .net unit-testing nunit

我有以下NUnit代码。我在[SetUp]

中使用了switch case语句

在测试运行之前,应该将数据插入数据库;但它没有插入数据。需要在这里纠正什么?

参考:

  1. https://softwareengineering.stackexchange.com/questions/173651/nunit-setup-and-teardown
  2. CODE

         [SetUp]
        public void TestSetUp()
        {
            switch (TestContext.CurrentContext.Test.FullName)
            {
                case "GetApplicationsTest":
                    GetApplicationsTestSETUPHElper();
                    break;
    
                case "DeleteApplicationTest":
                    GetApplicationsTestSETUPHElper();
                    break;
    
            }
    
    
        }
    
        private void GetApplicationsTestSETUPHElper()
        {
            SqlConnection connection = new SqlConnection(connectionString);
            using (connection)
            {
                string commandText = "INSERT INTO Status_Type VALUES (889,'s2','UT s2')";
                SqlCommand command = new SqlCommand(commandText, connection);
                command.CommandType = System.Data.CommandType.Text;
                connection.Open();
                command.ExecuteNonQuery();
            }
        }
    
        private void DeleteApplicationTestSETUPHElper()
        {
            SqlConnection connection = new SqlConnection(connectionString);
            using (connection)
            {
                string commandText = "INSERT INTO Status_Type VALUES (890,'s3','UT s3')";
                SqlCommand command = new SqlCommand(commandText, connection);
                command.CommandType = System.Data.CommandType.Text;
                connection.Open();
                command.ExecuteNonQuery();
            }
        }
    
        [Test]
        public void GetApplicationsTest()
        {
    
            ApplicationBL application = new ApplicationBL(connectionString);
            ApplicationSearch applicationSearch = new ApplicationSearch();
            applicationSearch.ApplicationID = 1;
            //applicationSearch.ApplicationName = "Test1";
            //applicationSearch.ApplicationURL = "Test1";
            //applicationSearch.StatusCode = 1;
            Collection<Application> applications =  application.GetApplications(applicationSearch);
        }
    
        [Test]
        public void DeleteApplicationTest()
        {
    
            ApplicationBL application = new ApplicationBL(connectionString);
            ApplicationSearch applicationSearch = new ApplicationSearch();
            applicationSearch.ApplicationID = 1;
            //applicationSearch.ApplicationName = "Test1";
            //applicationSearch.ApplicationURL = "Test1";
            //applicationSearch.StatusCode = 1;
            Collection<Application> applications = application.GetApplications(applicationSearch);
        }
    

1 个答案:

答案 0 :(得分:2)

TestContext.CurrentContext.Test.FullName

是测试的完全限定名称,而不仅仅是代码中的函数名称,因此修复应该是将测试的完全限定名称放在case语句中。