首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >编写参数化测试失败:检索测试用例的参数时出错:导致参数化字段的值无效

编写参数化测试失败:检索测试用例的参数时出错:导致参数化字段的值无效
EN

Stack Overflow用户
提问于 2012-03-27 20:17:18
回答 1查看 154关注 0票数 0

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

虽然我没有对不同的数据集运行相同的测试,但参数化测试似乎是一个可行的解决方案;然而,我很难让下面的示例工作起来。

注意:此代码基于http://docs.flexunit.org/index.php?title=Using_External_Data_for_Parameterized_Testing_And_Theories#TestNG_Style

代码语言:javascript
运行
复制
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的无效值

对于如何解决这个问题,有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-28 12:27:05

我找到的一个解决方案是使用Theories运行程序在类中运行测试,如下所示。

代码语言:javascript
运行
复制
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

而不是更有用的

代码语言:javascript
运行
复制
Error: Expected: a string containing "head"
but: was "this is some text"

虽然这样做可以“解决”我所遇到的问题,但IMHO在消息清晰度方面的权衡并不值得从外部源加载数据。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9897009

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档