如何使这个测试数据驱动?

huangapple go评论79阅读模式
英文:

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);
    }

huangapple
  • 本文由 发表于 2023年6月30日 00:29:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定