使用 NUnit 的测试用例

发布于 2024-08-16 19:36:41 字数 1501 浏览 1 评论 0原文

各位,我是为这些方法编写测试用例的新手。这里我有一个 InsertMethod,我想使用 NUnit 测试框架为其编写测试用例。帮助我编写以下方法的测试用例:

public bool insertUser(String FirstName, String LastName) { 布尔结果=假;

    SqlConnection myconn = new SqlConnection();
    SqlCommand mycmd = new SqlCommand();

    try
    {
        myconn.ConnectionString = "Data Source=BABU-PC;Initial Catalog=contacts;Integrated Security=True";

        myconn.Open();

        mycmd.Connection = myconn;
        mycmd.CommandText = "InsertUser";
        mycmd.CommandType = CommandType.StoredProcedure;


        SqlParameter param1 = new SqlParameter();
        param1.ParameterName = "@FirstName";
        param1.DbType = DbType.AnsiString;
        param1.Size = 8000;
        param1.Value = FirstName;
        mycmd.Parameters.Add(param1);


        SqlParameter param2 = new SqlParameter();
        param2.ParameterName = "@LastName";
        param2.DbType = DbType.AnsiString;
        param2.Size = 8000;
        param2.Value = LastName;
        mycmd.Parameters.Add(param2);

        int i = 0;

        i = mycmd.ExecuteNonQuery();

        if (i > 0)
        {
            result = true;
        }
        else
        {
            result = false;
        }


    }

    catch (Exception err)
    {
        Console.WriteLine(err.Message.ToString());

        return false;
    }

    finally
    {
        mycmd.Dispose();
        myconn.Close();
        myconn = null;

    }
    return result;
}

谢谢

SBM

Folks, I am new to writing testcases for the methods. Here I have a InsertMethod for which I want to write testcase using NUnit testing framework. Help me in writing the testcase for the method below :

public bool insertUser(String FirstName, String LastName)
{
bool result = false;

    SqlConnection myconn = new SqlConnection();
    SqlCommand mycmd = new SqlCommand();

    try
    {
        myconn.ConnectionString = "Data Source=BABU-PC;Initial Catalog=contacts;Integrated Security=True";

        myconn.Open();

        mycmd.Connection = myconn;
        mycmd.CommandText = "InsertUser";
        mycmd.CommandType = CommandType.StoredProcedure;


        SqlParameter param1 = new SqlParameter();
        param1.ParameterName = "@FirstName";
        param1.DbType = DbType.AnsiString;
        param1.Size = 8000;
        param1.Value = FirstName;
        mycmd.Parameters.Add(param1);


        SqlParameter param2 = new SqlParameter();
        param2.ParameterName = "@LastName";
        param2.DbType = DbType.AnsiString;
        param2.Size = 8000;
        param2.Value = LastName;
        mycmd.Parameters.Add(param2);

        int i = 0;

        i = mycmd.ExecuteNonQuery();

        if (i > 0)
        {
            result = true;
        }
        else
        {
            result = false;
        }


    }

    catch (Exception err)
    {
        Console.WriteLine(err.Message.ToString());

        return false;
    }

    finally
    {
        mycmd.Dispose();
        myconn.Close();
        myconn = null;

    }
    return result;
}

Thanks

SBM

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与往事干杯 2024-08-23 19:36:41

细胞鱼是对的。不要考虑代码,而要考虑该方法应该做什么。甚至不看你的代码,我就会做这样的测试:

1-准备

lastName = "LastName"
firstName = "FirstName"

然后尝试获取具有名字和姓氏的用户,并确保它不存在。

2- 执行

InsertUser(firstName, lastName)

3- 检查

  • 确保 InsertUser 返回 true
  • 尝试获取具有名字和姓氏的用户,并确保它具有正确的值。

Cellfish is right. Don't think about the code, think about what the method is supposed to do. Without even looking at your code, I'd do this kind of test:

1- Preparation

lastName = "LastName"
firstName = "FirstName"

Then try to fetch a user with firstName and lastName and make sure that it's not already there.

2- Execute

InsertUser(firstName, lastName)

3- Check

  • Make sure InsertUser return true
  • Try to fetch a user with firstName and lastName and make sure that it is there with the correct values.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文