我有几个单元测试,它们需要非常大的字符串来处理测试数据。我不想声明测试本身中的HTML字符串,因为这会掩盖实际的测试。相反,我希望为每个测试从外部资源加载这些字符串。
虽然我没有对不同的数据集运行相同的测试,但参数化测试似乎是一个可行的解决方案;然而,我很难让下面的示例工作起来。
注意:此代码基于http://docs.flexunit.org/index.php?title=Using_External_Data_for_Parameterized_Testing_And_Theories#TestNG_Style。
package flexUnitTests
{
import helpers.HTMLDataHelper;
import org.flexunit.runners.Parameterized;
import org.hamcrest.assertThat;
import org.hamcrest.text.containsString;
[RunWith("org.flexunit.runners.Parameterized")]
public class SimpleTestCase
{
private var parameterized:Parameterized;
public static var dataLoader:HTMLDataHelper = new HTMLDataHelper("data/layer.html");
[DataPoint(loader="dataLoader")]
public static var htmlContent:String;
[Test(dataprovider="htmlContent", description="Tests something.")]
public function mustPassThisSimpleTest(htmlContentParam:String):void
{
assertThat(htmlContentParam, containsString("head"));
}
}
}当我运行这个测试时,我会收到以下错误消息:
错误:检索testcase的参数时出错:导致参数化字段htmlContent: null的无效值
对于如何解决这个问题,有什么想法吗?
发布于 2012-03-28 12:27:05
我找到的一个解决方案是使用Theories运行程序在类中运行测试,如下所示。
package flexUnitTests
{
import helpers.HTMLDataHelper;
import org.flexunit.experimental.theories.Theories;
import org.flexunit.runners.Parameterized;
import org.hamcrest.assertThat;
import org.hamcrest.object.equalTo;
import org.hamcrest.text.containsString;
[RunWith("org.flexunit.experimental.theories.Theories")]
public class SimpleTestCase
{
public static var dataLoader:HTMLDataHelper = new HTMLDataHelper("data/layer.html");
[DataPoint(loader="dataLoader")]
public static var htmlContent:String;
[Test(dataprovider="htmlContent", description="Tests something.")]
public function mustPassThisSimpleTest(htmlContentParam:String):void
{
assertThat(htmlContentParam, containsString("head"));
}
}
}但是,副作用是当测试失败时,测试类中的所有测试都将显示神秘的错误消息。例如,
错误: mustWorkWithRegularTests
而不是更有用的
Error: Expected: a string containing "head"
but: was "this is some text"虽然这样做可以“解决”我所遇到的问题,但IMHO在消息清晰度方面的权衡并不值得从外部源加载数据。
https://stackoverflow.com/questions/9897009
复制相似问题