Specify a Test Suite for Your Project

Problem

You need to create a suite of tests that you can run using Pants' test goal.

Solution

For Scala or Java, create a junit_tests target definition that defines your suite; for Python, create a python_tests definition. For both target types, you need to specify:

  • A name for the Pants target
  • A list of sources specifying which files contain the tests themselves
  • A list of dependencies (which in many cases will include only the library being tested but could include others)
  • A list of resources (optional). More info can be found in Create a Resource Bundle

Below is an example junit_tests definition followed by a python_tests definition:

junit_tests(name='scala-tests',
  sources=rglobs('*.scala'),
  dependencies=[
    'myproject/src/main/scala',
  ]
)

python_tests(name='python-tests',
  sources=globs('*.py'),
  dependencies=[
    'myproject/src/python',
  ]
)

Now you can run the test using the Pants test goal like this:

$ ./pants test myproject/src/tests:scala-tests
Generated by publish_docs from dist/markdown/html/src/docs/common_tasks/test_suite.html 2017-08-18T17:29:55.408632