2

我开发了一个小型 Windows 窗体应用程序,它允许用户在华氏、摄氏度和开尔文三个温标的不同值之间进行转换。作为一名新手测试人员,我开始涉足 NUnit,我认为学习该框架的一个好方法是向该程序添加一些测试代码。处理大部分工作的课程是:-

public static class TemperatureConverter
{
    public static ConversionFormula CelciusToFahrenheit = t => ((t * 1.8) + 32);

    public static ConversionFormula CelciusToKelvin = t => (t + 273);

    public static ConversionFormula FahrenheitToCelcius = t => ((t - 32) * 5/9);

    public static ConversionFormula FahrenheitToKelvin = t => (t + 459.67)/1.8;

    public static ConversionFormula KelvinToCelcius = t => (t - 273);

    public static ConversionFormula KelvinToFahrenheit = t => (t * 9/5 - 459.67);


    public static double ConvertTemperature(double temperatureValue, ConversionFormula conversionFormula)
    {           
        return conversionFormula(temperatureValue);
    }
}

我首先简单地测试各个温度值是否正确转换。最终这变得很累,并且涉及到大量重复的代码,所以我没有为每个值编写一个测试,而是使用 NUnit 的“范围”和“值”属性,这样我就可以分十个批次进行测试。一个示例测试方法是:-

[TestFixture]
public class Class1
{
    private ConversionFormula _fahrenheitToCelcius;

    [SetUp]
    public void Init()
    {
        _fahrenheitToCelcius = TemperatureConverter.FahrenheitToCelcius;
    }

    #region Fahrenheit To Celcius Positive Values

    [Test, Sequential]
    public void ConvertFahrenheitToCelciusRange_0_To_10_RoundedTo2DP([Range(0, 10, 1)]double temperature,
                [Values(-17.78, -17.22, -16.67, -16.11, -15.56, -15, -14.44, -13.89, -13.33, -12.78, -12.22)]double expected)
    {
        double actual = TemperatureConverter.ConvertTemperature(temperature, _fahrenheitToCelcius);

        Assert.AreEqual(expected, actual, 1);
    }

范围值是要测试的温度,在本例中为 0 到 10。values 属性包含每个测试的预期结果。我正在使用 NUnit 2.6.2 和 Resharper 测试运行器。我在一个类文件中添加了几乎相同的测试方法,将温度范围提高了十倍。例如,下一个测试将是:-

[Test, Sequential]
    public void ConvertFahrenheitToCelciusRange_11_To_20_RoundedTo2DP([Range(11, 20, 1)]double temperature,
                [Values(-11.67, -11.11, -10.56, -10, -9.44, -8.89, -8.33, -7.78, -7.22, -6.67)]double expected)
    {
        double actual = TemperatureConverter.ConvertTemperature(temperature, _fahrenheitToCelcius);

        Assert.AreEqual(expected, actual, 1);
    }

添加每个测试后,我运行它以查看它是否通过并且确实通过了。最终,我最终测试了 0 到 150 的范围,因此每个班级总共有 15 种测试方法。现在出现问题。Resharper 测试运行器和 NUnit 测试运行器都随机报告

NUnit System.OverflowException:算术运算导致溢出

    System.OverflowException...

Server stack trace: 
   at NUnit.Framework.RangeAttribute..ctor(Int32 from, Int32 to, Int32 step)
   at System.Reflection.CustomAttribute._CreateCaObject(RuntimeModule pModule, IRuntimeMethodInfo pCtor, Byte** ppBlob, Byte* pEndBlob, Int32* pcNamedArgs)
   at System.Reflection.CustomAttribute.CreateCaObject(RuntimeModule module, IRuntimeMethodInfo ctor, IntPtr& blob, IntPtr blobEnd, Int32& namedArgs)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeParameterInfo parameter, RuntimeType caType)
   at NUnit.Core.Reflect.GetAttributes(ICustomAttributeProvider member, Boolean inherit)
   at NUnit.Core.Reflect.HasAttribute(ICustomAttributeProvider member, String attrName, Boolean inherit)
   at NUnit.Core.Builders.ValueSourceProvider.HasDataFor(ParameterInfo parameter)
   at NUnit.Core.Extensibility.DataPointProviders.HasDataFor(ParameterInfo parameter)
   at NUnit.Core.Builders.CombinatorialTestCaseProvider.HasTestCasesFor(MethodInfo method)
   at NUnit.Core.Extensibility.TestCaseProviders.HasTestCasesFor(MethodInfo method)
   at NUnit.Core.Builders.NUnitTestCaseBuilder.BuildFrom(MethodInfo method, Test parentSuite)
   at NUnit.Core.Extensibility.TestCaseBuilderCollection.BuildFrom(MethodInfo method, Test suite)
   at NUnit.Core.Builders.NUnitTestFixtureBuilder.BuildTestCase(MethodInfo method, TestSuite suite)
   at NUnit.Core.Builders.NUnitTestFixtureBuilder.AddTestCases(Type fixtureType)
   at NUnit.Core.Builders.NUnitTestFixtureBuilder.BuildSingleFixture(Type type, Attribute attr)
   at NUnit.Core.Builders.NUnitTestFixtureBuilder.BuildFrom(Type type)
   at NUnit.Core.Extensibility.SuiteBuilderCollection.BuildFrom(Type type)
   at NUnit.Core.TestFixtureBuilder.BuildFrom(Type type)
   at NUnit.Core.Builders.TestAssemblyBuilder.GetFixtures(Assembly assembly, String ns)
   at NUnit.Core.Builders.TestAssemblyBuilder.Build(String assemblyName, Boolean autoSuites)
   at NUnit.Core.Builders.TestAssemblyBuilder.Build(String assemblyName, String testName, Boolean autoSuites)
   at NUnit.Core.TestSuiteBuilder.BuildSingleAssembly(TestPackage package)
   at NUnit.Core.TestSuiteBuilder.Build(TestPackage package)
   at NUnit.Core.SimpleTestRunner.Load(TestPackage package)
   at NUnit.Core.ProxyTestRunner.Load(TestPackage package)
   at NUnit.Core.ProxyTestRunner.Load(TestPackage package)
   at NUnit.Core.RemoteTestRunner.Load(TestPackage package)
   at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Object[]& outArgs)
   at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at NUnit.Core.TestRunner.Load(TestPackage package)
   at NUnit.Util.TestDomain.Load(TestPackage package)
   at NUnit.Core.ProxyTestRunner.Load(TestPackage package)
   at NUnit.Util.RemoteTestAgent.AgentRunner.Load(TestPackage package)
   at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Object[]& outArgs)
   at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg)

Exception rethrown at [1]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at NUnit.Core.TestRunner.Load(TestPackage package)
   at NUnit.Core.ProxyTestRunner.Load(TestPackage package)
   at NUnit.Util.ProcessRunner.Load(TestPackage package)
   at NUnit.Util.TestLoader.LoadTest(String testName)

无论我尝试重新运行多少次测试,我都会得到同样的错误。不仅如此,程序集中所有其他运行良好的测试文件现在都报告了同样的问题。起初我认为这是传递给 Values 或 Range 属性的值的问题,但为了进行测试,我创建了一个全新的程序集,添加了对 NUnit 的引用,并从我的原始程序集中复制并粘贴了测试,到新的。嘿 presto,测试都运行良好。现在,如果我不更改新程序集中的测试文件并不时运行测试,最终我会遇到同样的错误。不仅如此,所有其他测试类也再次中断。解决这个问题的唯一方法是创建一个新的程序集并复制并过去旧的测试文件。这不好,因为它们最终会再次破裂。

4

2 回答 2

0

好吧,在一次流氓测试中,它似乎确实是一个不正确的范围值。复制和粘贴的危险。也就是说,我仍然不明白为什么它会影响程序集中的所有其他测试。即使是正确的测试在单独运行时也失败了,直到我纠正了错误。谢谢你的帮助。

于 2013-08-31T13:41:21.717 回答
0

好吧,当我们尝试在检查上下文中进行算术、强制转换或转换操作时,它定义了溢出异常。

于 2013-09-18T11:10:25.327 回答