英文:
How Would I Make This Test Data Driven?
问题
以下是翻译好的部分:
我有几个测试案例,它们之间唯一不同的是用户名,我想要对它们进行`TestCase[]`测试,但似乎没有什么效果。这是测试案例:
[Test]
public void SACCanSelectPayments()
{
LufasWebManager.OktaDevTenantSignIn(Users.lufas_sac1.ToString(), LufasWebManager.EncryptPassword(_site.GetAuditPassword(Users.lufas_sac1)));
new MenuBarSubPage(LufasWebManager.Driver).SelectPracticeAudit(MenuBarSubPage.PracticeAuditOptions.WardOrBranch);
LufasWebManager.Stall(2500);
LufasWebManager.Driver.FindElement(By.XPath("//simple-snack-bar/div/button")).Click();
var auditPage = new BaseAudit(LufasWebManager.Driver);
auditPage.ClickContinue();
auditPage.ClickContinue();
var paymentsPage = new AuditAddPaymentsDepositsSubpage(LufasWebManager.Driver);
SelectPayments(paymentsPage);
}
需要更改的地方在第一行的`Users.lufas_sac1`。我尝试过以下方法:
[TestCase("lufas_sac1")]
[Test]
public void SACCanSelectPayments(string user)
{
LufasWebManager.OktaDevTenantSignIn(Users.user.ToString(), LufasWebManager.EncryptPassword(_site.GetAuditPassword(Users.user)));
...
}
这不起作用,所以我尝试了字符串插值,但也不起作用。我不确定如何创建具有不同用户的测试案例,以避免重复测试。要保持DRY。
英文:
I have several tests where the only thing which is different from them is the username and I would like to do TestCase[]
with them but nothing seems to be working. Here is the test:
[Test]
public void SACCanSelectPayments()
{
LufasWebManager.OktaDevTenantSignIn(Users.lufas_sac1.ToString(), LufasWebManager.EncryptPassword(_site.GetAuditPassword(Users.lufas_sac1)));
new MenuBarSubPage(LufasWebManager.Driver).SelectPracticeAudit(MenuBarSubPage.PracticeAuditOptions.WardOrBranch);
LufasWebManager.Stall(2500);
LufasWebManager.Driver.FindElement(By.XPath("//simple-snack-bar/div/button")).Click();
var auditPage = new BaseAudit(LufasWebManager.Driver);
auditPage.ClickContinue();
auditPage.ClickContinue();
var paymentsPage = new AuditAddPaymentsDepositsSubpage(LufasWebManager.Driver);
SelectPayments(paymentsPage);
}
The thing which needs to change is in the first line the Users.lufas_sac1
. I have tried doing:
[TestCase("lufas_sac1")]
[Test]
public void SACCanSelectPayments(string user)
{
LufasWebManager.OktaDevTenantSignIn(Users.user.ToString(), LufasWebManager.EncryptPassword(_site.GetAuditPassword(Users.user)));
...
}
That didn't work so I tried string interpolation and that didn't work either. I am just not sure how to go about making test cases with the various users so I am not duplicating tests. Got to keep it DRY.
答案1
得分: 2
你的示例,至少按照发布的方式,没有使用你传递的参数。
参数的名称是'user'。它作为字符串传递给方法。你的TestCase
属性为其赋予了值“lufas_sac1”,我假设这是你想在测试中使用的用户名称。
另一方面,你的代码将Users.user
中保存的名称转换为字符串传递。当然,NUnit 对你的Users
类一无所知,并且不会对其进行初始化。
如果你将登录调用的第一个参数更改为简单的'user',那么你将使用传递的参数。但是,我怀疑你可能还需要对第二个登录参数进行更改。关于Users
类的信息不足,无法建议进一步的更改。
顺便说一下,作为一种风格的问题,当使用TestCase
时,我建议删除Test
属性。
英文:
Your example, at least as posted, doesn't use the parameter you are passing.
The name of the parameter is 'user'. It is passed to the method as a string. Your TestCase
attribute, gives it the value "lufas_sac1", which I assume is the name of the user you want to use for the test.
OTOH, your code is passing in the name held in Users.user
, converting it to a string. NUnit, of course, knows nothing abut your class Users
and does nothing to initialize it.
If you change the first argument to the login call to simply 'user' then you will make use of the passed-in argument. However, I suspect you will need to make changes to the second login argument as well. Not enough info here about the Users
class to suggest further changes.
BTW, as a matter of style, I'd drop the Test
attribute when using TestCase
.
答案2
得分: 0
这是我的工作代码,我将变量user
强制转换为类型Users
并使其正常工作。
[TestCase(Users.lufas_sac1)]
[Test]
public void UserCanSelectPayment(Users user)
{
LufasWebManager.OktaDevTenantSignIn(user.ToString(), LufasWebManager.EncryptPassword(_site.GetAuditPassword(user)));
new MenuBarSubPage(LufasWebManager.Driver).SelectPracticeAudit(MenuBarSubPage.PracticeAuditOptions.WardOrBranch);
LufasWebManager.Stall(2500);
LufasWebManager.Driver.FindElement(By.XPath("//simple-snack-bar/div/button")).Click();
var auditPage = new BaseAudit(LufasWebManager.Driver);
auditPage.ClickContinue();
auditPage.ClickContinue();
var paymentsPage = new AuditAddPaymentsDepositsSubpage(LufasWebManager.Driver);
SelectPayments(paymentsPage);
}
英文:
Here is my working code, I cast my variable user
as type Users
and got it working.
[TestCase(Users.lufas_sac1)]
[Test]
public void UserCanSelectPayment(Users user)
{
LufasWebManager.OktaDevTenantSignIn(user.ToString(), LufasWebManager.EncryptPassword(_site.GetAuditPassword(user)));
new MenuBarSubPage(LufasWebManager.Driver).SelectPracticeAudit(MenuBarSubPage.PracticeAuditOptions.WardOrBranch);
LufasWebManager.Stall(2500);
LufasWebManager.Driver.FindElement(By.XPath("//simple-snack-bar/div/button")).Click();
var auditPage = new BaseAudit(LufasWebManager.Driver);
auditPage.ClickContinue();
auditPage.ClickContinue();
var paymentsPage = new AuditAddPaymentsDepositsSubpage(LufasWebManager.Driver);
SelectPayments(paymentsPage);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论