你是如何扩展你的 Assert 类的

发布于 2024-07-09 21:10:05 字数 1449 浏览 5 评论 0原文

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

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

发布评论

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

评论(5

往日情怀 2024-07-16 21:10:05

这是我的解决方案:

using MyStuff;

using A = Microsoft.VisualStudio.TestTools.UnitTesting.Assert;

namespace Mytestproj.Tests
{
    public static class Assert
    {
        public static void AreEqual(object expected, object actual)
        {
            A.AreEqual(expected, actual);
        }

        // my extension
        public static void AreEqual(MyEnum expected, int actual)
        {
            A.AreEqual((int)expected, actual);
        }

        public static void IsTrue(bool o)
        {
            A.IsTrue(o);
        }

        public static void IsFalse(bool o)
        {
            A.IsFalse(o);
        }

        public static void AreNotEqual(object notExpected, object actual)
        {
            A.AreNotEqual(notExpected, actual);
        }

        public static void IsNotNull(object o)
        {
            A.IsNotNull(o);
        }

        public static void IsNull(object o)
        {
            A.IsNull(o);
        }
    }
}

That's my solution:

using MyStuff;

using A = Microsoft.VisualStudio.TestTools.UnitTesting.Assert;

namespace Mytestproj.Tests
{
    public static class Assert
    {
        public static void AreEqual(object expected, object actual)
        {
            A.AreEqual(expected, actual);
        }

        // my extension
        public static void AreEqual(MyEnum expected, int actual)
        {
            A.AreEqual((int)expected, actual);
        }

        public static void IsTrue(bool o)
        {
            A.IsTrue(o);
        }

        public static void IsFalse(bool o)
        {
            A.IsFalse(o);
        }

        public static void AreNotEqual(object notExpected, object actual)
        {
            A.AreNotEqual(notExpected, actual);
        }

        public static void IsNotNull(object o)
        {
            A.IsNotNull(o);
        }

        public static void IsNull(object o)
        {
            A.IsNull(o);
        }
    }
}
凉世弥音 2024-07-16 21:10:05

我喜欢 Assert 类的感觉,但想要一些可以更多地用作通用验证框架的东西。 我从 Roger Alsing 的文章 使用扩展方法,现在有一个系统,其工作原理如下:

Enforce.That(variable).IsNotNull();
Enforce.That(variable).IsInRange(10, 20);
Enforce.That(variable).IsTypeOf(typeof(System.String));
etc.

如果任何执行失败,它都会抛出异常。 我一直在考虑重构,以便我还可以合并不引发异常的非关键评估。 有些人喜欢将 Check.That 作为 Enforce.That 的变体,它会返回布尔值,但具有具有相同签名的扩展方法。

到目前为止,我喜欢这种方法的原因是,我可以在单元测试中使用这些方法,以及在实际代码中解决预验证和后验证问题,而无需引用 Microsoft.VisualStudio.QualityTools.UnitTestFramework 程序集。 我将它放在我的应用程序框架的根程序集中,Enforce 位于根目录中,因此很容易访问。

I like the feel of the Assert class, but wanted something that would serve more as a general validation framework. I started with Roger Alsing's article on using extension methods, and now have a system that works like:

Enforce.That(variable).IsNotNull();
Enforce.That(variable).IsInRange(10, 20);
Enforce.That(variable).IsTypeOf(typeof(System.String));
etc.

If any enforcement fails, it throws an exception. I have been considering refactoring so that I can also incorporate a non-critical evaluation that does not throw an exception. Some like Check.That as a variant of Enforce.That which would return booleans, but have extension methods with identical signatures.

What I like about this approach, so far, is that I can use these in my unit tests, as well as for pre-validation and post-validation concerns in my actual code without referencing the Microsoft.VisualStudio.QualityTools.UnitTestFramework assembly. I put it in my root assembly for my application framework, and Enforce is at the root, so it is very easy to get to.

挥剑断情 2024-07-16 21:10:05

我的很多测试都围绕着加载一个具有已知良好状态的对象(如 CuttingPath)。执行测试,然后将结果与加载的对象进行比较。 如果它们不同,那么“发生了”一些事情导致代码发生变化。

这种方法可以节省大量时间,并允许在需要时进行自定义比较。

A lot of my tests revolve around loading a object that (like a CuttingPath) with a known good state.. Performing the test, then comparing the result to the loaded object. If they differ then somehthing 'happened' to cause a change in the code.

This approach save tons of time and allow for custom comparisons when needed.

双手揣兜 2024-07-16 21:10:05

我认为,如果您重构测试以减少重复,那么您最终会创建自己的框架作为副产品,当然您的测试框架将具有在您的上下文中有意义的断言助手。

我想到的一个例子是,当测试 xhtml 报告时,我们最终得到的测试看起来像:

  assertCoverageEquals(45.5);

断言覆盖率后面的内容是这样的:

  assertPercentage(COVERAGE_ID, 45.5);

然后后面是使用 xpath 获取值的东西和另一种知道格式的方法用于百分比。

I think that if you're refactoring your tests to reduce duplication then you end up creating your own framework as a by-product, and of course your test framework will have assert helpers that make sense in your context.

An example I have in mind was when testing xhtml report we ended up with tests that looked something like:

  assertCoverageEquals(45.5);

where behind assert coverage was something like:

  assertPercentage(COVERAGE_ID, 45.5);

and then behind that was something that used xpath to get the value and another method that knew what the formatting was for percentages.

摘星┃星的人 2024-07-16 21:10:05

我刚刚添加了一个实现 ImageAssert 正如我上面所写(在我的问题中)
我很高兴听到更多此类样本

[TestMethod]
public void CompareImagesSize()
{
 Image expected = Bitmap.FromFile(@"C:\ShaniData\Projects2008\TddSamples\Output\ExpectedImage.png");
 Image actual = Bitmap.FromFile(@"C:\ShaniData\Projects2008\TddSamples\Output\RhinoDiagram.png");

 Bitmap expectedBitmap = new Bitmap(expected);
 Bitmap actualBitmap = new Bitmap(actual);

 ImageAssert.HasTheSameSize(expectedBitmap, actualBitmap);
}

[TestMethod]
public void CompareTwoSameImagesButWithDifferenExtension()
{
 Image expected = Bitmap.FromFile(@"C:\ShaniData\Projects2008\TddSamples\Output\Image2.png");
 Image actual = Bitmap.FromFile(@"C:\ShaniData\Projects2008\TddSamples\Output\Image1.jpg");

 Bitmap expectedBitmap = new Bitmap(expected);
 Bitmap actualBitmap = new Bitmap(actual);

 ImageAssert.AreEqual(expectedBitmap, actualBitmap);
}

public class ImageAssert
{
    //public static void MoreMethods(Bitmap expected, Bitmap actual)
    //{
    //    //Compare image extensions
    //    //Compare Thumbnail...
    //}

    public static void HasTheSameSize(Bitmap expected, Bitmap actual)
    {
        if ((expected.Height != actual.Height)
            || (expected.Width != actual.Width))
            HandleFail("ImageAssert.HasTheSameSize", String.Empty);
    }

    public static void AreEqual(Bitmap expected, Bitmap actual)
    {
        for (int i = 0; i < expected.Width; i++)
        {
            for (int j = 0; j < expected.Height; j++)
            {
                Color expectedBit = expected.GetPixel(i, j);
                Color actualBit = actual.GetPixel(i, j);
                if (!expectedBit.Equals(actualBit))
                {
                    HandleFail("ImageAssert.AreEqual", String.Empty, i, j);
                    return;
                }
            }
        }
    }

    internal static void HandleFail(string assertionName, string message, params object[] parameters)
    {
        throw new AssertFailedException(String.Format(assertionName));
    }
}

I've just added an implementation to ImageAssert as I wrote above (in my question)
I would be glad to hear more of that kind of samples

[TestMethod]
public void CompareImagesSize()
{
 Image expected = Bitmap.FromFile(@"C:\ShaniData\Projects2008\TddSamples\Output\ExpectedImage.png");
 Image actual = Bitmap.FromFile(@"C:\ShaniData\Projects2008\TddSamples\Output\RhinoDiagram.png");

 Bitmap expectedBitmap = new Bitmap(expected);
 Bitmap actualBitmap = new Bitmap(actual);

 ImageAssert.HasTheSameSize(expectedBitmap, actualBitmap);
}

[TestMethod]
public void CompareTwoSameImagesButWithDifferenExtension()
{
 Image expected = Bitmap.FromFile(@"C:\ShaniData\Projects2008\TddSamples\Output\Image2.png");
 Image actual = Bitmap.FromFile(@"C:\ShaniData\Projects2008\TddSamples\Output\Image1.jpg");

 Bitmap expectedBitmap = new Bitmap(expected);
 Bitmap actualBitmap = new Bitmap(actual);

 ImageAssert.AreEqual(expectedBitmap, actualBitmap);
}

public class ImageAssert
{
    //public static void MoreMethods(Bitmap expected, Bitmap actual)
    //{
    //    //Compare image extensions
    //    //Compare Thumbnail...
    //}

    public static void HasTheSameSize(Bitmap expected, Bitmap actual)
    {
        if ((expected.Height != actual.Height)
            || (expected.Width != actual.Width))
            HandleFail("ImageAssert.HasTheSameSize", String.Empty);
    }

    public static void AreEqual(Bitmap expected, Bitmap actual)
    {
        for (int i = 0; i < expected.Width; i++)
        {
            for (int j = 0; j < expected.Height; j++)
            {
                Color expectedBit = expected.GetPixel(i, j);
                Color actualBit = actual.GetPixel(i, j);
                if (!expectedBit.Equals(actualBit))
                {
                    HandleFail("ImageAssert.AreEqual", String.Empty, i, j);
                    return;
                }
            }
        }
    }

    internal static void HandleFail(string assertionName, string message, params object[] parameters)
    {
        throw new AssertFailedException(String.Format(assertionName));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文