如何设置一个将bool作为输入的TestCase



我想做两个测试用例,评估它们的布尔值。

public string ActivateAgent(bool trueorfalse)
{
agentActivationStatus = true;
agentPriviledge = true;
return $"Agent activation status {agentActivationStatus}, SHD priviledges {agentPriviledge}, Agent {surname} is now active";
}

我只用整数和字符串做过单元测试,从来没有用bool。这是我试图创建的测试用例:


[TestCase(true)]
public void AgentIsActivated(bool expected)
{
bool result = Agent.ActivateAgent(bool true);
Assert.AreEqual(expected, result);
}

我只想测试代理是否已激活。

这是Agent的完整类,非常基础,但我是C#的新手。


public class Agent
{
string agentID;
public bool agentActivationStatus = false; //activated yes no
bool agentPriviledge = false; //has agent priviledges
int waveStatus;
public bool agentStatus = false;
public DateTime agentLastSeen = DateTime.Now; //agent last seen alive
public readonly string forename; //agent first name
public readonly string surname; //agent last name;
//CONSTRUCTORS --------------------------------------------------
public Agent(string fname, string lname)
{
forename = fname;
surname = lname;
string fullname = fname + " " + lname;
}
//METHODS --------------------------------------------------------
public string ActivateAgent(bool trueorfalse)
{
agentActivationStatus = true;
agentPriviledge = true;
return $"Agent activation status {agentActivationStatus}, SHD priviledges {agentPriviledge}, Agent {surname} is now active";
} 
public string DeactivateAgent(bool trueorfalse)
{
agentActivationStatus = false;
agentPriviledge = false;
return $"Agent activation status {agentActivationStatus}, SHD priviledges {agentPriviledge}, Agent {surname} is now unactive.";
}
public string getAgentStatus()
{
return $"Agent activation is {agentActivationStatus}, Agent is active";
}
//public string getAgentStatus()
//{
//    //string result = " ";
//    //DateTime lastSeen30Days = agentLastSeen.AddDays(30);
//    //if (agentLastSeen > lastSeen30Days && agentPriviledge ==)
//    //{
//    //   result = $"Agent was last seen {lastSeen30Days}, Agent is presumed MIA ";
//    //}
//    //else if (agentStatus == false && agentPriviledge == true)
//    //{
//    //    result = $"Agent was last seen {agentLastSeen} deceased.";
//    //}
//    //else
//    //{
//    //    result = $"Agent was last seen {agentLastSeen} Alive";
//    //}
//    //return result;
}

您可以使用IsTrue(或IsFalse(

[Test]
public void AgentIsActivated()
{
bool activateAgent = true;
bool result = Agent.ActivateAgent(activateAgent);
Assert.IsTrue(result);
}

您的示例代码几乎很好。TestCase适用于您想要参数化的测试。

[TestCase(true)]
public void AgentIsActivated(bool expected)
{
var agent = new Agent("abc", "def");
bool result = agent.ActivateAgent(expected);
Assert.AreEqual(expected, result);
}

若不需要传递参数,那个么可以使用Test属性。

[Test]
public void AgentIsActivated()
{
var agent = new Agent("abc", "def");
bool result = agent.ActivateAgent(true);
Assert.IsTrue(result);
}

相关内容

  • 没有找到相关文章

最新更新