Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Visual Studio 2015中进行单元测试_C#_Unit Testing_Testing_Visual Studio 2015_Nunit - Fatal编程技术网

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

C# 如何在Visual Studio 2015中进行单元测试,c#,unit-testing,testing,visual-studio-2015,nunit,C#,Unit Testing,Testing,Visual Studio 2015,Nunit,我喜欢下面 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 C

我喜欢下面

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 void GetBranchListForErrorInput()
还需要绿色的勾号。


我该怎么做呢?

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

但对于您的案例,最简单的方法如下

[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);
}
[TestMethod]
public void TestGetBranchList()
{
字符串projectId=“someProjectId”;
var result=branchs.GetBranchList(projectId);
//如果结果具有预期结果,则断言。
}
[测试方法]
public void TestGetProjectList()
{
var result=branchs.GetProjectList();
//如果结果具有预期结果,则断言。
}
[测试方法]
公共void TestCreateBranch()
{
//准备测试数据
List projectName=新列表{“someProject”};
字符串sourceBranch=“sourceBranch”
字符串destinationBranch=“destBranch”;
Branches branchesObj=新分支();
//通过传递测试数据调用方法。
branchesObj.CreateBranch(项目名称、源分支、目标分支);
}

这将有助于您解决问题。

没有太大区别。除了调用类上的方法而不是类的实例之外,您可以像测试其他方法一样测试它们。你试过了吗?@ChetanRanpariya是的,我现在知道答案了!谢谢,我得到了确切的答案。我认为您可能在
branchs.GetBranchList
Oh.中传递了错误的三个参数。。对我的错。。。我更正了答案。。谢谢你指出..你需要声明不平等。类似于
Assert.NotEqual
CreateBranch
的方法只返回
void
,所以您实际上无法在对其进行单元测试时执行断言。您只需在单元测试中调用该方法,就可以看到它不会因为该方法中任何未处理的异常而中断。我更新了答案。在直接进入单元测试之前,您需要花一些时间学习单元测试的基础知识。这里有很多博客和文章。
[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);
}