输出是另一个的输入的单元测试方法

发布于 2024-12-11 17:15:21 字数 1665 浏览 0 评论 0原文

我总是尝试在每次测试中坚持一个断言,但有时我在这样做时遇到了麻烦。

例如。

假设我编写了一个加密和解密字符串的加密类。

public class CryptoDummy
{
    public string Decrypt(string value)
    {
    }

    public string Encrypt(string value)
    {
    }
}

如果解密取决于加密的输出,我将如何创建单元测试?

到目前为止,我的大多数测试(如果不是全部)都是由每个测试一个方法调用和每个测试一个断言组成的。

那么,对于每个测试进行多次调用并断言我最后调用的方法得出的最终结果是否可以?

public class CryptoDummyTest
{
    private static CryptoDummy _cryptoDummy;

    // Use ClassInitialize to run code before running the first test in the class
    [ClassInitialize]
    public static void MyClassInitialize(TestContext testContext)
    {
        _cryptoDummy = new CryptoDummy();
    }

    [TestMethod]
    public void Encrypt_should_return_ciphered_64string_when_passing_a_plaintext_value()
    {
        const string PLAINTEXT_VALUE = "[email protected]";

        string cipheredString = _cryptoDummy.Encrypt(PLAINTEXT_VALUE);

        Assert.IsTrue(cipheredString != PLAINTEXT_VALUE);
    }

    [TestMethod]
    public void Decrypt_should_return_plaintext_when_passing_a_ciphered_value()
    {
        const string PLAINTEXT_VALUE = "[email protected]";

        string cipheredString = _cryptoDummy.Encrypt(PLAINTEXT_VALUE);

        string plaintextString = _cryptoDummy.Decrypt(cipheredString);

        Assert.IsTrue(plaintextString == PLAINTEXT_VALUE);
    }
}

先感谢您。

I always try to stick to one assertion per test but sometimes I'm having troubles in doing so.

For example.

Say I've written a cryptographic class that encrypt and decrypts strings.

public class CryptoDummy
{
    public string Decrypt(string value)
    {
    }

    public string Encrypt(string value)
    {
    }
}

How would I create my unit test if the decryption is depended upon the output of the encryption ?

Most of my tests if not all up until now are composed of one method call per test and one assertion per test.

So to the point, is it fine to have multiple calls per test and assert the final results made by the method I called last ?

public class CryptoDummyTest
{
    private static CryptoDummy _cryptoDummy;

    // Use ClassInitialize to run code before running the first test in the class
    [ClassInitialize]
    public static void MyClassInitialize(TestContext testContext)
    {
        _cryptoDummy = new CryptoDummy();
    }

    [TestMethod]
    public void Encrypt_should_return_ciphered_64string_when_passing_a_plaintext_value()
    {
        const string PLAINTEXT_VALUE = "[email protected]";

        string cipheredString = _cryptoDummy.Encrypt(PLAINTEXT_VALUE);

        Assert.IsTrue(cipheredString != PLAINTEXT_VALUE);
    }

    [TestMethod]
    public void Decrypt_should_return_plaintext_when_passing_a_ciphered_value()
    {
        const string PLAINTEXT_VALUE = "[email protected]";

        string cipheredString = _cryptoDummy.Encrypt(PLAINTEXT_VALUE);

        string plaintextString = _cryptoDummy.Decrypt(cipheredString);

        Assert.IsTrue(plaintextString == PLAINTEXT_VALUE);
    }
}

Thank you in advance.

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

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

发布评论

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

评论(3

弄潮 2024-12-18 17:15:21

您不应该让一项测试依赖于另一项测试。最好的方法是将加密文本输出到某处并保存。然后在解密文本测试中,您可以从加密文本开始并测试您是否正确解密它。如果您使用相同的加密密钥(这对于测试来说很好),加密的字符串将始终是相同的。因此,将第二个单元测试更改为如下所示:

[TestMethod]
public void Decrypt_should_return_plaintext_when_passing_a_ciphered_value()
{

    const string PLAINTEXT_VALUE = "[email protected]";

    string cipheredString = "sjkalsdfjasdljs"; // ciphered value captured

    string plaintextString = _cryptoDummy.Decrypt(cipheredString);

    Assert.IsTrue(plaintextString == PLAINTEXT_VALUE);
}

You shouldnt have one test depending upon another. The best way to do this would be to output the encrypted text somewhere and save it. Then on the decrypt text test you could start with an encrypted text and test you decrypt it correctly. If you use the same encryption key (which is fine for testing) the encrypted string will always be the same. So change your second unit test to something like this:

[TestMethod]
public void Decrypt_should_return_plaintext_when_passing_a_ciphered_value()
{

    const string PLAINTEXT_VALUE = "[email protected]";

    string cipheredString = "sjkalsdfjasdljs"; // ciphered value captured

    string plaintextString = _cryptoDummy.Decrypt(cipheredString);

    Assert.IsTrue(plaintextString == PLAINTEXT_VALUE);
}
顾铮苏瑾 2024-12-18 17:15:21

这对我来说听起来很奇怪。我对单元测试的看法是,单元测试将使用提供的一组明确的数据来测试一种特殊情况。如果一项测试依赖于另一项测试的结果,则结果是不确定的。第二件事是,您无法确定测试的执行顺序!

This sounds strange to me. My opinion of unit testing is, that a unit test will test one special situation with a definite set of data provided. If one test depends on the result of another test, the result is not deterministic. The second thing is, that you can not be asured of the order the tests are executed!

无人问我粥可暖 2024-12-18 17:15:21

我并不那么虔诚地说每个测试只能有一个断言。例如,如果要测试的结果包含某种树结构,则必须断言树中的每个阶段都是正确的,从而导致多个断言,因为(在我看来)为每个步骤编写是没有意义的一次测试。

另外,在您给出的示例中,我看不到您的最后一个测试依赖于任何其他测试。它只是调用被测单元两次,实际上您对它如何加密和解密数据并不真正感兴趣。您感兴趣的只是它是否有效。所以对于那种你的测试你的测试绝对没问题。

如果您需要测试用于解密和加密的算法,则必须进行两次测试并将结果与​​一些预定义的常量进行比较,以确保没有人会更改所使用的算法。

I'm not so religious to say that you can have only one assert per test. If your result to test for example contains some kind of tree structure, you'll have to assert that every stage in the tree is correct, thous leading to multiple asserts, cause it makes (in my eyes) no sense to write for every step a single test.

Also in your given example i can't see that your last test depends on any other test. It simply calls the unit under test two times and indeed you are not really interested on how it encrypt and decrypt the data. All you are interested in, is that it works. So for that kind your tests your tests are absolutely okay.

If you need to test the algorithm used for decryption and encryption you'll have to make two tests and compare the results with some pre-defined constants to make sure that nobody is going to change the algorithm used.

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