MIT licensed by Freckle R&D
Maintained by [email protected]
This version can be pinned in stack with:hspec-junit-formatter-1.1.2.1@sha256:d45e8a6389339dc9e0213387e875a7e52545b1895632b20280942f383715542b,5842

hspec-junit-formatter

Hackage Stackage Nightly Stackage LTS CI

A JUnit XML runner/formatter for hspec.

Usage (with hspec-discover)

Place the following in test/SpecHook.hs:

import Test.Hspec
import Test.Hspec.JUnit.Config
import qualified Test.Hspec.JUnit.Formatter as Formatter

hook :: Spec -> Spec
hook = Formatter.use $ defaultJUnitConfig "test-suite"

This replaces the usual formatter, so only a JUnit report is generated and no other output is visible.

Registering instead of using

To make the JUnit formatter available for use with --format, but not used by default, use register:

hook2 :: Spec -> Spec
hook2 = Formatter.register $ defaultJUnitConfig "test-suite"

Adding a JUnit report

To produce a JUnit report in addition to normal output, use add:

hook3 :: Spec -> Spec
hook3 = Formatter.add $ defaultJUnitConfig "test-suite"

Environment Configuration

To configure things via @JUNIT_@-prefixed environment variables, import Formatter.Env instead. It exports all the same functions:

import qualified Test.Hspec.JUnit.Formatter.Env as FormatterEnv

And set the necessary variables,

JUNIT_OUTPUT_DIRECTORY=/tmp
JUNIT_SUITE_NAME=my-tests
hook4 :: Spec -> Spec
hook4 = FormatterEnv.add

Environment Enabling

To only apply a hook if JUNIT_ENABLED=1, wrap it in whenEnabled:

JUNIT_ENABLED=1
hook5 :: Spec -> Spec
hook5 = FormatterEnv.whenEnabled FormatterEnv.add

Without hspec-discover

Hooks are just functions of type Spec -> Spec, so you can apply them right before calling hspec in main:

main :: IO ()
main = hspec $ FormatterEnv.whenEnabled FormatterEnv.add spec

spec :: Spec
spec = describe "Addition" $ do
  it "adds" $ do
    2 + 2 `shouldBe` (4 :: Int)

Golden Testing

This project’s test suite uses [hspec-golden][] to generate an XML report for Example.hs and then compare that with golden XML files checked into the repository. If your work changes things in a functionally-correct way, but that diverges from the golden XML files, you need to regenerate them.

  1. Run rm tests/golden*.xml
  2. Run the specs again

We maintain specific golden XML files for GHC 8.x vs 9.x, so you will need to re-run the test suite with at least one of each series to regenerate all the necessary files.


LICENSE

Changes

Unreleased

v1.1.2.1

Relax version bounds

v1.1.2.0

  • Replace the string {base} by the basename of the current directory (typically the package name) when reading any JUNIT environment variable values.

v1.1.1.0

  • Add Test.Hspec.JUnit.Formatter{,Env}, for use as a spec hook
  • Drop support for hspec < 0.10

v1.1.0.2

  • Support GHCs 9.0 and 9.2

v1.1.0.1

  • Release with looser lower-bounds
  • CI against older GHCs

v1.1.0.0

  • Remove incorrectly-cased Test.HSpec modules

v1.0.3.0

  • Add hspecJUnit and environment-variable configuration
  • Fix file attribute not respecting source-path-prefix

v1.0.2.2

  • Add file and line attributes in testcase nodes

v1.0.2.1

  • Support for prefixing reported source paths (e.g. if in a monorepo)
  • Ensure tests pass within unpacked release tarball

v1.0.2.0

  • Create Test.Hspec module-space and deprecate misspelled Test.HSpec modules
  • Introduce configWithJUnit and JUnitConfig

v1.0.1.0

  • Format function can be used directly without withConfig or runJUnitSpec.
  • Test case duration is now supported.
  • Failure locations are listed for some result types.
  • Timestamps in the resulting XML now display the start time of formatting.

v1.0.0.4

  • Bring base bound back in to fix release

v1.0.0.3

  • Remove dependencies upper bounds

v1.0.0.2

  • Less restrictive upper bound on base

v1.0.0.1

  • Bump base dep

v1.0.0.0

Initial release.