我开发了一个小型的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的“Range”和“Values”属性,所以我可以分批测试10个。一个示例测试方法是: -
[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 Runner。我在一个类文件中添加了几乎相同的测试方法,这些方法将温度范围提高了十倍。例如,下一个测试将是: -
[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,测试一切正常。现在,如果我不对新程序集中的测试文件进行任何更改并不时运行测试,最终我会遇到同样的错误。不仅如此,所有其他测试类也会再次中断。解决这个问题的唯一方法是创建一个新的程序集并复制并遍历旧的测试文件。这不好,因为他们最终会再次破裂。我是NUnit newby,所以如果有人之前遇到过这个错误或者能够理解我在这里做错了什么,那么你的建议将不胜感激。
答案 0 :(得分:0)
嗯,在一次流氓测试中看起来确实是一个不正确的范围值。复制和粘贴的危险。也就是说,我仍然不明白为什么它会影响装配中的所有其他测试。单独运行时,即使正确的测试也会失败,直到我纠正错误。谢谢你的支持。
答案 1 :(得分:0)
那么当我们尝试在已检查的上下文中进行算术,转换或转换操作时,它定义了溢出异常。