简体   繁体   English

XSLT 单元测试

[英]XSLT Unit testing

Does anyone know of a way to write unit tests for the XSLT transformation?有谁知道为 XSLT 转换编写单元测试的方法?

I've a lot of XSLT files and it's getting harder to test them manually.我有很多 XSLT 文件,手动测试它们变得越来越困难。 We have an example XML and can compare it to the resulting output XML from the XSL transormation.我们有一个示例 XML,可以将它与 XSL 转换的结果输出 XML 进行比较。 However, I'm looking for a better test method.但是,我正在寻找更好的测试方法。

I am currently looking for some good options to do this as well.我目前也在寻找一些好的选择来做到这一点。 As a result, I came across this question, and a few other potential candidate solutions.结果,我遇到了这个问题,以及其他一些潜在的候选解决方案。 Admittedly, I haven't tried any of them yet, so I can't speak to their quality, but at least they are some other avenues potentially worthy of researching.不可否认,我还没有尝试过它们中的任何一个,所以我不能说它们的质量,但至少它们是一些其他值得研究的途径。

  1. Jenni Tennison's Unit Testing Package Jenni Tennison 的单元测试包
  2. UTF-X Unit Testing Framework UTF-X 单元测试框架
  3. Juxy朱西
  4. XTC XTC

Additionally, I found the following article to be informative in terms of a general methodology for unit testing XSLT.此外,我发现以下文章在单元测试 XSLT 的一般方法方面提供了丰富的信息。

Unit test XSL transformations单元测试 XSL 转换

Try XSpec , a testing framework for XSLT.试试XSpec ,一个用于 XSLT 的测试框架。 It allows you to write tests declaratively, and test templates and functions.它允许您以声明方式编写测试,并测试模板和函数。

Looks like Oxygen editor has Unit Testing available as well.看起来 Oxygen 编辑器也有单元测试可用。 It " provides XSLT Unit Test support based on XSpec ".它“提供基于 XSpec 的 XSLT 单元测试支持”。

I haven't tried it myself, but will soon.我自己还没有尝试过,但很快就会。

Here are a few simple solutions:这里有几个简单的解决方法:

  • Use xsltproc with a mock XML file:将 xsltproc 与模拟 XML 文件一起使用:

     xsltproc test.xsl mock.xml
  • XSLT Cookbook - Chapter 13 XSLT 手册 - 第 13 章

  • Create a document() placeholder variable and comment/uncomment it manually:创建一个document()占位符变量并手动注释/取消注释:

     <xsl:variable name="Data" select="descendant-or-self::node()"/> <!-- <xsl:variable name="Data" select="document('foo.xml')" /> --> <xsl:if test="$Data/pagename='foo'"> <p>hi</p> </xsl:if>
  • Create a condition to swap the comment programmatically:创建一个条件以编程方式交换评论:

     <xsl:variable name="Data"> <xsl:choose> <!-- If source XML is inline --> <xsl:when test="descendant-or-self::node()/pageName='foo'"/> <xsl:value-of select="descendant-or-self::node()"/> </xsl:when> <!-- If source XML is external --> <xsl:otherwise> <xsl:value-of select="document('foo.xml')" /> </xsl:otherwise> </xsl:choose> </xsl:variable>

Use a shell script to inline the data programmatically in the build to automate the tests completely.使用 shell 脚本在构建中以编程方式内联数据以完全自动化测试。

References参考

We have been using Java based unit test cases, in which we provide expected xml string after transformation and input xml string which needs to be transformed using some XSL.我们一直在使用基于 Java 的单元测试用例,其中我们提供转换后的预期 xml 字符串和需要使用一些 XSL 转换的输入 xml 字符串。 Refer to following package if you want to explore more.如果您想探索更多,请参阅以下包。

org.custommonkey.xmlunit.Transform
org.custommonkey.xmlunit.Diff
org.custommonkey.xmlunit.DetailedDiff

I´m using this tool: jxsltunit .我正在使用这个工具: jxsltunit

The test is defined by an XML file which is then passed to the tool.该测试由一个 XML 文件定义,然后该文件被传递给该工具。 This is an example of the test configuration:这是测试配置的示例:

<xsltTestsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="jxsltunit jxslttestsuite.xsd" xmlns="jxsltunit"
    description="Testsuite Test"
    xml="min-test.xml"
    xslt="min-test.xslt"
    path="pa > ch">
    <xsltTestcase match_number="0">
        <![CDATA[<ch>child 1</ch>]]>
    </xsltTestcase>
    <xsltTestcase match_number="1">
        <![CDATA[<ch>child 2</ch>]]>
    </xsltTestcase>
</xsltTestsuite>

It takes the XML, the XSL and a path in the transformed XML which gets tested.它需要 XML、XSL 和经过测试的转换后的 XML 中的路径。 The path can contain a list which elements are identified by their index.路径可以包含一个列表,其中元素由它们的索引标识。

One benefit of this tool is that it can output the results as a junit XML file.这个工具的一个好处是它可以将结果输出为一个 junit XML 文件。 This file can be picked up by your Jenkins to show the XLST-tests in your test results.您的 Jenkins 可以获取此文件以在您的测试结果中显示 XLST 测试。 Just add the call to the tool as a build step.只需将调用添加到工具作为构建步骤。

Try Jenni Tennison's Unit Testing Package (XSpec), which is a unit test and behaviour-driven development (BDD) framework for XSLT, XQuery, and Schematron.试试 Jenni Tennison 的单元测试包 (XSpec),它是一个用于 XSLT、XQuery 和 Schematron 的单元测试和行为驱动开发 (BDD) 框架。 It is based on the Spec framework of RSpec, which is a BDD framework for Ruby.它基于 RSpec 的 Spec 框架,它是 Ruby 的 BDD 框架。

With XSpec you can test XLT template wise or XPath wise per your need.使用 XSpec,您可以根据需要测试 XLT 模板明智或 XPath 明智。 For an overview on how to use/handle/write (installation|execution) click https://github.com/xspec/xspec/wiki/What-is-XSpec有关如何使用/处理/写入(安装|执行)的概述,请单击https://github.com/xspec/xspec/wiki/What-is-XSpec

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM