无法访问另一个项目的变量
我正在尝试使用 C# (Restsharp) 自动化 API 测试。我在同一解决方案中有 2 个项目。一个项目是一个控制台应用程序,我试图在其中保留方法,而另一个项目是一个单元测试项目,我计划在其中保留测试用例。
我可以通过创建方法的对象来访问 BASE 项目中的方法,但无法访问该方法中的变量来应用断言。知道我错过了什么吗? (我是 c# 新手)。
的 Base 项目
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using RestSharp;
namespace Base
{
public class Methods
{
public void GetMethod(long ID)
{
var client = new RestClient("getUrl");
var request = new RestRequest(Method.GET);
request.AddParameter("userId", ID);
IRestResponse response = client.Execute(request);
HttpStatusCode statusCode = response.StatusCode;
int nStatusCode = (int)statusCode;
}
}
}
测试项目 UnitTest 中
using System;
using System.Net;
using Base;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RestSharp;
namespace Tests
{
[TestClass]
public class UnitTests
{
[TestMethod]
public void StatusOK()
{
//Arrange, Act, Assert
var methods = new Methods();
methods.GetMethod(2330013)
Assert.IsTrue(nStatusCode.Equals(200));
}
}
}
UnitTest 类中的方法类无法读取 nStatusCode 变量以及响应变量。 我已经添加了项目之间的依赖关系。 感谢您的帮助!
I am trying to automate API testing using c# (Restsharp). I have 2 projects in same solution. One project is a console app where I am trying to keep the methods, while the other one is a Unit Test Project where I plan on keeping the test cases.
I am able to access the methods from the BASE project by creating an object of the method, but unable to access the variables within that method to apply assertions. Any idea what am I missing?
(I am new to c#).
Method class in Base project
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using RestSharp;
namespace Base
{
public class Methods
{
public void GetMethod(long ID)
{
var client = new RestClient("getUrl");
var request = new RestRequest(Method.GET);
request.AddParameter("userId", ID);
IRestResponse response = client.Execute(request);
HttpStatusCode statusCode = response.StatusCode;
int nStatusCode = (int)statusCode;
}
}
}
UnitTest class in Test project
using System;
using System.Net;
using Base;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RestSharp;
namespace Tests
{
[TestClass]
public class UnitTests
{
[TestMethod]
public void StatusOK()
{
//Arrange, Act, Assert
var methods = new Methods();
methods.GetMethod(2330013)
Assert.IsTrue(nStatusCode.Equals(200));
}
}
}
UnitTest is unable to read nStatusCode variable, as well as response variable.
I have already added dependency between the projects.
Appreciate the help!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决该问题的一种选择是让 GetMethod 返回状态代码:
然后测试可以检查返回值。
附言。我建议使用
Assert.AreEqual
而不是Assert.IsTrue
,因为在代码不是 200 的情况下,测试失败输出将包含错误值。使用IsTrue
你只会知道它不是 200,但你不会知道它是什么。One option to address the problem is to make GetMethod return the status code:
Then the test can examine the return value.
PS. I recommend using
Assert.AreEqual
instead ofAssert.IsTrue
because in the case the code is not 200 the test failure output will contain the bad value. WithIsTrue
you will only know that it wasn't 200 but you won't know what it was.