0

我有一些单元测试需要非常大的测试数据字符串。我不想在测试本身中声明 HTML 字符串,因为这会掩盖实际测试。相反,我想为每个测试从外部资源加载这些字符串。

尽管我没有使用不同的数据集运行相同的测试,但参数化测试看起来是一个可行的解决方案;但是,我很难让以下示例正常工作。

注意:此代码基于TestNG 示例

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"));
        }
    }
}

当我运行此测试时,我收到以下错误消息:

错误:检索测试用例的参数时出错:导致参数化字段 htmlContent 的值无效:null

关于这个问题的解决方案可能是什么?

4

1 回答 1

0

我发现的一种解决方案是使用 runner 在班级中运行测试,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"

尽管这确实“解决了”我遇到的问题,但恕我直言,在消息清晰度方面的权衡不值得能够从外部来源加载数据。

于 2012-03-28T12:27:05.670 回答