一些 Nunit 测试在组中失败,但在其他环境中单独 运行 时通过
Some Nunit tests are failing in groups but pass when run individually in other env
我一直在尝试直接点击 Web 控制器以使用 Nunit、Specflow 和 Restsharp 上传 csv 文件,但测试在组中失败,但在单独时通过。我们注意到的另一件事是,这些在本地运行良好,如 运行 应用程序作为本地主机,但在其他环境中则不然。所有其他测试组都在那个环境中通过。我看过一些建议不要使用静态属性来存储值的帖子,但我也试过了。我们还在线程之间添加了延迟,但这也不起作用
任何人都可以帮助解决这个问题或任何遇到过这个问题的人吗?
我得到的错误是超时。如果你从浏览器访问它就没有问题
我正在使用上下文注入在 step def 文件中相互传递值。
这是代码的样子
public (IRestResponse response,string RVtoken) GetRVtoken()
{
var client = new RestClient(ConfigurationManager.AppSettings["Web_URL"] +"Account/SignIn");
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
var html = new HtmlDocument();
html.LoadHtml(response.Content.ToString());
var RVtoken = html.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[1]/div[1]/form[1]/input[1]/@value[1]").Attributes["value"].Value;
return (response, RVtoken);
}
public (CookieContainer cookieContainer, Uri target) GetAspxAuthToken(string email,string password, IRestResponse SignInResponse, string RVtoken)
{
string ReturnUrl = ConfigurationManager.AppSettings["Web_URL"] + "Account/SignIn?ReturnUrl=%2F";
var client = new RestClient(ReturnUrl);
CookieContainer cookieContainer = new CookieContainer();
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(ReturnUrl);
httpRequest.CookieContainer = cookieContainer;
httpRequest.Method = Method.POST.ToString();
string postData = string.Format("Email={0}&Password={1}&__RequestVerificationToken={2}&RememberMe=false", email, password, RVtoken);
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.ContentLength = byteArray.Length;
Uri target = new Uri(ReturnUrl);
httpRequest.CookieContainer.Add(new Cookie(SignInResponse.Cookies[0].Name, SignInResponse.Cookies[0].Value) { Domain = target.Host });
httpRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
Stream dataStream = httpRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
HttpWebResponse myresponse = (HttpWebResponse)httpRequest.GetResponse();
return (cookieContainer,target);
}
public IRestResponse Uploadflex(string flexType,string filepath,IRestResponse SignInResponse)
{
RestClient newclient = newclient = new RestClient(ConfigurationManager.AppSettings["Web_URL"] + "audiences/" + flexType + "/upload?utc=1580117555355");
var newrequests = new RestRequest(Method.POST);
newrequests.AddFile("targetAreaFile",filepath);
newrequests.AddCookie(SignInResponse.Cookies[0].Name, SignInResponse.Cookies[0].Value);
newrequests.AddCookie(User.cookieContainer.GetCookies(User.target)[".aspxauth"].Name.ToString(), User.cookieContainer.GetCookies(User.target)[".aspxauth"].Value.ToString());
newrequests.AddParameter("name", "targetAreaFile");
newrequests.AddParameter("Content-Type", "application/vnd.ms-excel");
newrequests.AddParameter("Content-Disposition", "form-data");
var response = newclient.Execute(newrequests);
return response;
}
好的,所以问题是我正在为这些测试创建不同的会话,但不知何故 cookie 没有清除,所以我将所有测试放在同一个会话下,它神奇地工作。
我一直在尝试直接点击 Web 控制器以使用 Nunit、Specflow 和 Restsharp 上传 csv 文件,但测试在组中失败,但在单独时通过。我们注意到的另一件事是,这些在本地运行良好,如 运行 应用程序作为本地主机,但在其他环境中则不然。所有其他测试组都在那个环境中通过。我看过一些建议不要使用静态属性来存储值的帖子,但我也试过了。我们还在线程之间添加了延迟,但这也不起作用
任何人都可以帮助解决这个问题或任何遇到过这个问题的人吗? 我得到的错误是超时。如果你从浏览器访问它就没有问题
我正在使用上下文注入在 step def 文件中相互传递值。 这是代码的样子
public (IRestResponse response,string RVtoken) GetRVtoken()
{
var client = new RestClient(ConfigurationManager.AppSettings["Web_URL"] +"Account/SignIn");
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
var html = new HtmlDocument();
html.LoadHtml(response.Content.ToString());
var RVtoken = html.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[1]/div[1]/form[1]/input[1]/@value[1]").Attributes["value"].Value;
return (response, RVtoken);
}
public (CookieContainer cookieContainer, Uri target) GetAspxAuthToken(string email,string password, IRestResponse SignInResponse, string RVtoken)
{
string ReturnUrl = ConfigurationManager.AppSettings["Web_URL"] + "Account/SignIn?ReturnUrl=%2F";
var client = new RestClient(ReturnUrl);
CookieContainer cookieContainer = new CookieContainer();
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(ReturnUrl);
httpRequest.CookieContainer = cookieContainer;
httpRequest.Method = Method.POST.ToString();
string postData = string.Format("Email={0}&Password={1}&__RequestVerificationToken={2}&RememberMe=false", email, password, RVtoken);
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.ContentLength = byteArray.Length;
Uri target = new Uri(ReturnUrl);
httpRequest.CookieContainer.Add(new Cookie(SignInResponse.Cookies[0].Name, SignInResponse.Cookies[0].Value) { Domain = target.Host });
httpRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
Stream dataStream = httpRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
HttpWebResponse myresponse = (HttpWebResponse)httpRequest.GetResponse();
return (cookieContainer,target);
}
public IRestResponse Uploadflex(string flexType,string filepath,IRestResponse SignInResponse)
{
RestClient newclient = newclient = new RestClient(ConfigurationManager.AppSettings["Web_URL"] + "audiences/" + flexType + "/upload?utc=1580117555355");
var newrequests = new RestRequest(Method.POST);
newrequests.AddFile("targetAreaFile",filepath);
newrequests.AddCookie(SignInResponse.Cookies[0].Name, SignInResponse.Cookies[0].Value);
newrequests.AddCookie(User.cookieContainer.GetCookies(User.target)[".aspxauth"].Name.ToString(), User.cookieContainer.GetCookies(User.target)[".aspxauth"].Value.ToString());
newrequests.AddParameter("name", "targetAreaFile");
newrequests.AddParameter("Content-Type", "application/vnd.ms-excel");
newrequests.AddParameter("Content-Disposition", "form-data");
var response = newclient.Execute(newrequests);
return response;
}
好的,所以问题是我正在为这些测试创建不同的会话,但不知何故 cookie 没有清除,所以我将所有测试放在同一个会话下,它神奇地工作。