我做了一个简单的小练习,但它在我目前的经历之外。我对视觉工作室和Nunits都很陌生,基本上我创建了一个简单的数据库和一个用C#编写的插入/删除/更新它的类,我努力做的是让NUnits为我工作来测试我写的方法。
我已经通过NuGet安装了NUnit 3.0和NUnit适配器,并且已经打开了一个新项目,并添加了我的解决方案,其中我已经编写了一个我编写测试的类库,这里我有点迷失了。我编写的测试没有出现在测试资源管理器中,我已经尝试了每个可以找到的源来解释为什么没有成功,我已经改变了x86和x64之间的处理器架构设置,并且清理和重建等但没有。任何帮助或提示将非常感谢,并提前非常感谢。
这些是方法
namespace Employees
{
public class DatabaseConnection
{
private string connectionString="bla blah blah";
SqlConnection connection;
public string read()
{
string query = "SELECT LAST(first_name) FROM Employee";
using (connection = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand(query, connection))
using (SqlDataAdapter adaptor = new SqlDataAdapter(cmd)) //Reperesents a set of data commands and a database connection
{
connection.Open();
string name = (string)cmd.ExecuteScalar();
return name;
}
}
public void insertEntry(string first_name,string last_name, string title, string dept)
{
string query = "INSERT INTO Employees VALUES ('@first_name','@last_name','@job_title','@department')";
using (connection = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand(query, connection))
using (SqlDataAdapter adaptor = new SqlDataAdapter(cmd)) //Reperesents a set of data commands and a database connection
{
cmd.Parameters.AddWithValue("@first_name", first_name);
cmd.Parameters.AddWithValue("@last_name", last_name);
cmd.Parameters.AddWithValue("@job_title", title);
cmd.Parameters.AddWithValue("@department", dept);
connection.Open();
cmd.ExecuteNonQuery();
}
}
public void updateEntry(int id, string first_name, string last_name, string title, string dept)
{
string query = "UPDATE Employees SET first_name='@first_name', last_name='@last_name', job_title='@job_title', department='@department' WHERE id = '@Employeeid";
using (connection = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand(query, connection))
using (SqlDataAdapter adaptor = new SqlDataAdapter(cmd)) //Reperesents a set of data commands and a database connection
{
cmd.Parameters.AddWithValue("@first_name", first_name);
cmd.Parameters.AddWithValue("@last_name", last_name);
cmd.Parameters.AddWithValue("@job_title", title);
cmd.Parameters.AddWithValue("@department", dept);
cmd.Parameters.AddWithValue("@Employeeid", id);
connection.Open();
cmd.ExecuteNonQuery();
}
}
public void deleteEntry(int id)
{
string query = "DELETE FROM Employees WHERE id = @Employeeid";
using (connection = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand(query, connection))
using (SqlDataAdapter adaptor = new SqlDataAdapter(cmd)) //Reperesents a set of data commands and a database connection
{
cmd.Parameters.AddWithValue("@Employeeid", id);
connection.Open();
cmd.ExecuteNonQuery();
}
}
}
}
我计划使用的示例测试用例:
namespace TestLib.NUnitTest
{
using NUnit.Framework;
using Employees;
[TestFixture]
public class TestClass
{
[Test]
public void insertSuccess()
{
string a = "Levi";
string b = "Smith";
string c = "Sales Assistant";
string d = "Sales";
DatabaseConnection con = new DatabaseConnection();
con.insertEntry(a, b, c, d);
string n = con.read();
Assert.AreEqual(a, n);
}
[Test]
public void insertFail()
{
string a = "Levi";
string b = "Smith";
string c = "Sales Assistant";
string d = "Sales";
DatabaseConnection con = new DatabaseConnection();
con.insertEntry(a, b, c, d);
string n = con.read();
Assert.AreEqual(a, n);
}
[Test]
public void updateSuccess()
{
Assert.Pass("This is a pass");
//DatabaseConnection con = new DatabaseConnection();
//Assert.That(con.insertEntry(a, b, c, d));
}
[Test]
public void updateFail()
{
if (true)
Assert.Fail("This is a failure");
}
[Test]
public void deleteSuccess()
{
Assert.Pass("This is a pass");
//DatabaseConnection con = new DatabaseConnection();
//Assert.That(con.insertEntry(a, b, c, d));
}
[Test]
public void deleteFail()
{
if (true)
Assert.Fail("This is a failure");
}
}
}