NUnit测试用例输入问题

时间:2013-03-23 16:49:19

标签: visual-studio-2010 c#-4.0 nunit

我的代码从文件中获取输入。如果我必须为它编写NUnit测试用例,我是否必须更改我的代码并从测试用例而不是文件中获取输入,或者我可能不必更改我的代码。

请帮助。

以下是我必须测试的功能之一。

    public void CLEAN_Discipline(string disp)
    {
        //////////////////////////////////////////////////////SQL CONNECTION////////////////////////////////////////////////

        string sqlconnectionString = "server=localhost;" + "database=fastdata;" + "uid=dwh;" + "password=dwh;";
        MySqlConnection cnMySQL = new MySqlConnection(sqlconnectionString);
        MySqlCommand cmdMySQL = cnMySQL.CreateCommand();
        cmdMySQL.CommandText = "Select * from single";
        MySqlDataReader reader;
        String query;
        MySqlCommand cmd2;
        cnMySQL.Open();
        reader = cmdMySQL.ExecuteReader(); 


        while (reader.Read())
        {
            string disp = reader["Discipline"].ToString();

            disp = disp.ToUpper();
            disp = disp.Replace("MS", "");
            disp = disp.Replace("-", "");
            disp = disp.Replace(" ", "");

            string roll = reader["RollNumber"].ToString();


            MySqlConnection cnMySQL2 = new MySqlConnection(sqlconnectionString);
            cnMySQL2.Open();
            query = "UPDATE single SET Discipline = ('" + disp + "')  where RollNumber = '" + roll + "' ";
            cmd2 = new MySqlCommand(query, cnMySQL2);
            cmd2.ExecuteNonQuery();
            cnMySQL2.Close();
        }
        cnMySQL.Close();

        cnMySQL.Open();
        reader = cmdMySQL.ExecuteReader();
        while (reader.Read())
        {
            string roll = reader["RollNumber"].ToString();
            string disp = reader["Discipline"].ToString();
            string degree = reader["Degree"].ToString();
            if (degree == "MS")
            {
                if (roll[0] == 'I')
                {
                    if (disp != "CS" && disp != "SPM" && disp != "")
                    {
                        disp = "UNKNOWN";
                    }
                }
                else if (roll[0] == 'K')
                {
                    if (disp != "CS" && disp != "SPM" && disp != "TC" && disp != "NW")
                    {
                        disp = "UNKNOWN";
                    }
                }
                else if (roll[0] == 'P')
                {
                    if (disp != "CS" && disp != "TC")
                    {
                        disp = "UNKNOWN";
                    }
                }
                else if (roll[0] == 'L')
                {
                    if (disp != "CS" && disp != "SPM" && disp != "TC" && disp != "NW")
                    {
                        disp = "UNKNOWN";
                    }
                }
            }
            if (disp == "UNKNOWN")
            {
                MySqlConnection cnMySQL2 = new MySqlConnection(sqlconnectionString);
                cnMySQL2.Open();
                query = "UPDATE single SET Discipline = ('" + disp + "')  where RollNumber = '" + roll + "' ";
                cmd2 = new MySqlCommand(query, cnMySQL2);
                cmd2.ExecuteNonQuery();
                cnMySQL2.Close();
            }

        }
    }

1 个答案:

答案 0 :(得分:0)

我建议你将你的功能分解成更小的功能,这样你就可以只测试包含逻辑的部分。

在您的情况下,您可能应该提取逻辑,将要检查的字符串的各个方面替换为带有字符串的函数。然后,您可以在没有所有其他依赖项的情况下测试该函数(读取和写入数据库)。

Michael Feathers的

Working Effectively with Legacy Code是解释如何在代码中创建允许测试的接缝的绝佳资源。