GraemeF
GraemeF

Reputation: 11457

How do I perform automated testing of XSLT stylesheets?

I have some increasingly complex XSLT stylesheets and it would be helpful if I could run some tests on them as part of my CI build process, and even use TDD to develop them in the first place. I'm currently using Visual Studio to run fragments of XML through the stylesheets and I am manually checking the results.

What would everyone recommend for this? Ideally it would be easy to integrate into CruiseControl.NET and/or MsBuild.

Upvotes: 12

Views: 2176

Answers (5)

Kathy Van Stone
Kathy Van Stone

Reputation: 26271

What I have done is used my standard unit testing system with a good library for testing output. At my current project, the output is XHTML and I am using JUnit and xml-unit. At a previous project, the output was XSL-FO and I used python-unit and xmllib. This allows me to build the XSLT gradually (using TDD) by having a single test check only part of the output. If the output is text, however, I might compare the entire result. I used my current unit testing software for two reasons. First, I was familiar with it so it was faster to get started. Second, it was very easy for the testing to be automated with the build if it was already using the type of test the build was expecting.

Upvotes: 5

Meiscooldude
Meiscooldude

Reputation: 3739

I work on a project that uses XSL-T. We have a few data-driven tests running with visual studio. I also believe N-Unit has some data driven test features.

Upvotes: 1

Jan Willem B
Jan Willem B

Reputation: 3806

If you are familiar with Apache Cocoon, you could use CoUnit which uses xslt-unit under the hood.

Testcases look like this:

<testcase id="03-reverse" ignore-whitespace="true">
  <input>
    <text-to-reverse> 
      The text in this element 
      <embedded-element/> 
      will be reversed.
    </text-to-reverse>
  </input>
  <expect>
    <text-to-reverse> 
      tnemele siht ni txet ehT 
      <embedded-element/> 
      .desrever eb lliw
    </text-to-reverse>
  </expect>
  <xslt src="reverse.xsl"/>
</testcase>

Upvotes: 2

Grzenio
Grzenio

Reputation: 36639

I think I would write unit tests for them in your programming language of choice (e.g. C#). Have a collection of input xmls and corresponding expected outputs and just run the xsl on these and make sure they match the outputs. I am not sure if there is a smarter way of doing the testing.

Upvotes: 2

Related Questions