1

我有以下 NUnit 代码。我在 [SetUp] 中使用 switch case 语句

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

参考:

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

代码

     [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);
    }
4

1 回答 1

2
TestContext.CurrentContext.Test.FullName

是测试的完全限定名称,而不仅仅是代码中的函数名称,因此解决方法应该是将测试的完全限定名称放在案例语句中。

于 2012-11-05T10:48:10.587 回答