Skip to content

Using Third-Party Libraries

nf-test supports including third party libraries (e.g. jar files ) or functions from groovy files to either extend it functionality or to avoid duplicate code and to keep the logic in test cases simple.

Using Local Groovy Files

0.7.0 ·

If nf-test detects a lib folder in the directory of a tescase, then it adds it automatically to the classpath.

Examples

We have a Groovy script MyWordUtils.groovy that contains the following class:

class MyWordUtils {

    def static capitalize(String word){
      return word.toUpperCase();
    }

}

We can put this file in a subfolder called lib:

testcase_1
├── capitalizer.nf
├── capitalizer.test
└── lib
    └── MyWordUtils.groovy

The file capitalizer.nf contains the CAPITALIZER process:

#!/usr/bin/env nextflow
nextflow.enable.dsl=2

process CAPITALIZER {
    input:
        val cheers
    output:
        stdout emit: output
    script:
       println "$cheers".toUpperCase()
    """
    """

}

Next, we can use this class in the capitalizer.nf.test like every other class that is provided by nf-test or Groovy itself:

nextflow_process {

    name "Test Process CAPITALIZER"
    script "capitalizer.nf"
    process "CAPITALIZER"

    test("Should run without failures") {

        when {
            process {
                """
                input[0] = "world"
                """
            }
        }

        then {
            assert process.success
            assert process.stdout.contains(MyWordUtils.capitalize('world'))
        }

    }

}

If we have a project and we want to reuse libraries in multiple test cases, then we can store the class in the shared lib folder. Both test cases are now able to use MyWordUtils:

tests
├── testcase_1
    ├── hello_1.nf
    ├── hello_1.nf.test
├── testcase_2
    ├── hello_2.nf
    ├── hello_2.nf.test
└── lib
    └── MyWordUtils.groovy

The default location is tests/lib. This folder location can be changed in nf-test config file.

It is also possible to use the --lib parameter to add an additional folder to the classpath:

nf-test test tests/testcase_1/hello_1.nf.test --lib tests/mylibs

If multiple folders are used, the they need to be separate with a colon (like in Java or Groovy).

Using Local Jar Files

To integrate local jar files, you can either specify the path to the jar within the nf-test --lib option

nf-test test test.nf.test --lib tests/lib/groovy-ngs-utils/groovy-ngs-utils.jar

or add it as follows to the nf-test.config file:

libDir "tests/lib:tests/lib/groovy-ngs-utils/groovy-ngs-utils.jar"

You could then import the class and use it in the then statement:

import gngs.VCF;

nextflow_process {

    name "Test Process VARIANT_CALLER"
    script "variant_caller.nf"
    process "VARIANT_CALLER"

    test("Should run without failures") {

        when {
           ...
        }

        then {
            assert process.success             
            def vcf = VCF.parse("$baseDir/tests/test_data/NA12879.vcf.gz")
            assert vcf.samples.size() == 10
            assert vcf.variants.size() == 20
        }

    }

}

Using Maven Artifcats with @Grab

nf-test supports the @Grab annotation to include third-party libraries that are available in a maven repository. As the dependency is defined as a maven artifact, there is no local copy of the jar file needed and maven enables to include an exact version as well as provides an easy update process.

Example

The following example uses the WordUtil class from commons-lang:

@Grab(group='commons-lang', module='commons-lang', version='2.4')
import org.apache.commons.lang.WordUtils

nextflow_process {

    name "Test Process CAPITALIZER"
    script "capitalizer.nf"
    process "CAPITALIZER"

    test("Should run without failures") {

        when {
            process {
                """
                input[0] = "world"
                """
            }
        }

        then {
            assert process.success
            assert process.stdout.contains(WordUtils.capitalize('world'))
        }

    }

}