FitNesse 中的异常处理

发布于 2024-09-06 08:04:55 字数 1335 浏览 6 评论 0原文

Gojko Adzic,在他的书中使用 FitNesse 进行测试驱动的 .NET 开发,给出了一些减少测试代码的基本技巧。有一个名为 LogIn 的方法,它根据用户名和密码返回玩家的 id,或者在没有这样的注册玩家时抛出异常。这是测试代码的初始版本:

public class CheckLogIn : fit.ColumnFixture
{
    public string username;
    public string password;

    public int LoggedInAsPlayerId()
    {
        try
        {
            SetUpTestEnvironment.playerManager.LogIn(username, password);
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }
}

然后用较短的版本替换:

public class CheckLogIn : fit.ColumnFixture
{
    public string username;
    public string password;

    public int LoggedInAsPlayerId()
    {
        return SetUpTestEnvironment.playerManager.LogIn(username, password);
    }
}

现在第二个测试有一个额外的好处,可以验证是否返回正确的 id,但它不允许您检查系统是否拒绝未知的用户/密码错误。 是否有任何特殊值可以在列中使用来指示异常?或者我是否必须结合这两个测试才能覆盖所有场景?我记得,尽管此时此刻还模糊,有某种用于处理异常的单元测试模式,但我确信有人已经问过它,所以我不想重复。除非社区对此表示同意,否则请建议测试此类方法的最佳实践。

如果我没有说清楚:

假设我有一个注册玩家:john.doe/test123/101(用户名/密码/id)。我想要测试系统的两个组合是 john.doe/test123/101john.doe/johnny/

Gojko Adzic, in his book Test Driven .NET Development with FitNesse, gives some basic tips on reducing the test code. There's a method called LogIn which, based on the username and password, returns player's id or throws an exception when there's no such registered player. Here's the initial version of the test code:

public class CheckLogIn : fit.ColumnFixture
{
    public string username;
    public string password;

    public int LoggedInAsPlayerId()
    {
        try
        {
            SetUpTestEnvironment.playerManager.LogIn(username, password);
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }
}

This is then replaced with a shorter version:

public class CheckLogIn : fit.ColumnFixture
{
    public string username;
    public string password;

    public int LoggedInAsPlayerId()
    {
        return SetUpTestEnvironment.playerManager.LogIn(username, password);
    }
}

Now the second test has an added bonus of verifying if a correct id is returned, but it doesn't allow you to check if the system rejects an unknown user/wrong password. Is there any special value which can be used in a column to indicate an exception? Or do I have to combine both tests to cover all scenarios? I remember, albeit vaguely at this very moment, there was some kind of unit test pattern for handling exceptions, but I'm sure somebody has already asked about it, so I don't want to duplicate. Unless the community is OK with that, then please suggest a best practice for testing such methods.

In case I'm not being clear:

Say I got one registered player: john.doe/test123/101 (username/password/id). Two of the combinations I would want to test the system against are john.doe/test123/101 and john.doe/johnny/<WrongUserOrPasswordException>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

非要怀念 2024-09-13 08:04:56

有两个固定关键字允许检查异常:

  • error - 在测试期间应抛出异常
  • exception - 检查特定异常

这实际上在这本书的下一页...我很遗憾没有立即注意到它。也许有人会发现这些信息有用,所以我不会删除这个问题。

There are two fixture keywords which allow checking for exceptions:

  • error - when an exception should be thrown during the test
  • exception - to check for a particular exception

This was actually well explained on the following page of the book... My bad for not noticing it straight away. Maybe somebody will find this information useful, so I'm not deleting the question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文