如何在Visual Studio 2015中进行单元测试

时间:2017-04-07 07:17:15

标签: c# unit-testing testing visual-studio-2015 nunit

我喜欢以下。

Select 
    b.ProductID, c.ProductName,
    (Select 
         Case 
            When SUM(Qty) IS NULL 
               then 0 
               else SUM(Qty) 
         end 
     from 
         InvoiceDetails 
     where 
         ProductID = b.ProductID) as Sold, 
    (Select 
         Case 
            When SUM(QtyReceive) IS NULL 
               then 0 
               else SUM(QtyReceive) 
         end 
     from 
         PurchaseOrderDetails 
     where 
         ProductID = b.ProductID) as Stocks,
     ((Select 
           Case 
              When SUM(QtyReceive) IS NULL 
                 then 0 
                 else SUM(QtyReceive) 
           end 
       from 
           PurchaseOrderDetails 
       where 
           ProductID = b.ProductID) - 
      (Select 
           Case 
              When SUM(Qty) IS NULL 
                 then 0 
                 else SUM(Qty) 
           end 
       from 
           InvoiceDetails 
       where 
           ProductID = b.ProductID)) as RemainingStock  
from 
    InvoiceDetails a  
Right join 
    PurchaseOrderDetails b on a.ProductID = b.ProductID  
Inner join 
    Products c on b.ProductID = c.ProductID  
Group By 
    b.ProductID, c.ProductName  

我能够测试该方法并获得正输出。但我不知道如何测试这两种方法using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; namespace Syncfusion.Gitlab { public class Branches { public void CreateBranch(List<string> projectName, string sourceBranch, string destinationBranch) { } public void CreateTag(List<string> projectName, string sourceBranch,string tagname) { } public static List<string> GetBranchList(string projectId) { } public static List<ProjectData> GetProjectList() { } } public class ExcelOperation { public void GenerateExcel(List<ProjectDetails> finalExcelData, List<string>projectUrl,List<string>tagsorBranchUrl) { } } }

我的示例测试代码如下。以下方法已成功通过public static List<string> GetBranchList(string projectId), public static List<ProjectData> GetProjectList()测试。

NUnit

我该如何测试这两种方法?

更新

我可以在第一个答案的帮助下得到答案。但现在我有一个疑问。

我如何测试输入错误?我的意思是我知道我给出的输入是错误的,但我需要绿色刻度标记进行测试。这意味着给出的输入是错误的,因此输出也是错误的,因此测试是正确的。

在我的下图中。我还需要[TestMethod] public void CreateTags() { List<string> project = new List<string>(); project.Add("test1"); string sourceBranch = "master"; string tagsName = "v1.0.0"; branch.CreateTag(project, sourceBranch, tagsName); } 绿色刻度线。  enter image description here

我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

单元测试静态方法与测试非静态方法非常相似。根据静态方法中的逻辑,它可能会变得复杂。

但对你的情况最简单的方法如下。

[TestMethod]
public void TestGetBranchList()
{
    string projectId = "someProjectId";
    var result = Branches.GetBranchList(projectId);
    //Assert if result has expected result.
}

[TestMethod]
public void TestGetProjectList()
{
    var result = Branches.GetProjectList();
    //Assert if result has expected result.
}

[TestMethod]
public void TestCreateBranch()
{
    //Prepare TestData
    List<string> projectName = new List<string> {"someProject"};
    string sourceBranch = "sourceBranch"
    string destinationBranch = "destBranch";

    Branches branchesObj = new Branches();
    // Call method by passing the test data.
    branchesObj.CreateBranch(projectName, sourceBranch, destinationBranch);
}

这可以帮助您解决问题。