opa: github.com/open-policy-agent/opa/tester Index | Files

package tester

import "github.com/open-policy-agent/opa/tester"

Package tester contains utilities for executing Rego tests.

Index

Package Files

reporter.go runner.go

Constants

const TestPrefix = "test_"

TestPrefix declares the prefix for all test rules.

func Load Uses

func Load(args []string, filter loader.Filter) (map[string]*ast.Module, storage.Store, error)

Load returns modules and an in-memory store for running tests.

func LoadBundles Uses

func LoadBundles(args []string, filter loader.Filter) (map[string]*bundle.Bundle, error)

LoadBundles will load the given args as bundles, either tarball or directory is OK.

type BenchmarkOptions Uses

type BenchmarkOptions struct {
    ReportAllocations bool
}

BenchmarkOptions defines options specific to benchmarking tests

type JSONCoverageReporter Uses

type JSONCoverageReporter struct {
    Cover     *cover.Cover
    Modules   map[string]*ast.Module
    Output    io.Writer
    Threshold float64
}

JSONCoverageReporter reports coverage as a JSON structure.

func (JSONCoverageReporter) Report Uses

func (r JSONCoverageReporter) Report(ch chan *Result) error

Report prints the test report to the reporter's output. If any tests fail or encounter errors, this function returns an error.

type JSONReporter Uses

type JSONReporter struct {
    Output io.Writer
}

JSONReporter reports test results as array of JSON objects.

func (JSONReporter) Report Uses

func (r JSONReporter) Report(ch chan *Result) error

Report prints the test report to the reporter's output.

type PrettyReporter Uses

type PrettyReporter struct {
    Output                   io.Writer
    Verbose                  bool
    FailureLine              bool
    BenchmarkResults         bool
    BenchMarkShowAllocations bool
    BenchMarkGoBenchFormat   bool
}

PrettyReporter reports test results in a simple human readable format.

func (PrettyReporter) Report Uses

func (r PrettyReporter) Report(ch chan *Result) error

Report prints the test report to the reporter's output.

type Reporter Uses

type Reporter interface {

    // Report is called with a channel that will contain test results.
    Report(ch chan *Result) error
}

Reporter defines the interface for reporting test results.

type Result Uses

type Result struct {
    Location        *ast.Location            `json:"location"`
    Package         string                   `json:"package"`
    Name            string                   `json:"name"`
    Fail            bool                     `json:"fail,omitempty"`
    Error           error                    `json:"error,omitempty"`
    Duration        time.Duration            `json:"duration"`
    Trace           []*topdown.Event         `json:"trace,omitempty"`
    FailedAt        *ast.Expr                `json:"failed_at,omitempty"`
    BenchmarkResult *testing.BenchmarkResult `json:"benchmark_result,omitempty"`
}

Result represents a single test case result.

func Run Uses

func Run(ctx context.Context, paths ...string) ([]*Result, error)

Run executes all test cases found under files in path.

func RunWithFilter Uses

func RunWithFilter(ctx context.Context, filter loader.Filter, paths ...string) ([]*Result, error)

RunWithFilter executes all test cases found under files in path. The filter will be applied to exclude files that should not be included.

func (Result) Pass Uses

func (r Result) Pass() bool

Pass returns true if the test case passed.

func (*Result) String Uses

func (r *Result) String() string

type Runner Uses

type Runner struct {
    // contains filtered or unexported fields
}

Runner implements simple test discovery and execution.

func NewRunner Uses

func NewRunner() *Runner

NewRunner returns a new runner.

func (*Runner) EnableFailureLine Uses

func (r *Runner) EnableFailureLine(yes bool) *Runner

EnableFailureLine if set will provide the exact failure line

func (*Runner) EnableTracing Uses

func (r *Runner) EnableTracing(yes bool) *Runner

EnableTracing enables tracing of evaluation and includes traces in results. Tracing is currently mutually exclusive with coverage.

func (*Runner) Filter Uses

func (r *Runner) Filter(regex string) *Runner

Filter will set a test name regex filter for the test runner. Only test cases which match the filter will be run.

func (*Runner) Run Uses

func (r *Runner) Run(ctx context.Context, modules map[string]*ast.Module) (ch chan *Result, err error)

Run executes all tests contained in supplied modules. Deprecated: Use RunTests and the Runner#SetModules or Runner#SetBundles helpers instead. This will NOT use the modules or bundles set on the Runner.

func (*Runner) RunBenchmarks Uses

func (r *Runner) RunBenchmarks(ctx context.Context, txn storage.Transaction, options BenchmarkOptions) (ch chan *Result, err error)

RunBenchmarks executes tests similar to tester.Runner#RunTests but will repeat a number of times to get stable performance metrics.

func (*Runner) RunTests Uses

func (r *Runner) RunTests(ctx context.Context, txn storage.Transaction) (ch chan *Result, err error)

RunTests executes tests found in either modules or bundles loaded on the runner.

func (*Runner) SetBundles Uses

func (r *Runner) SetBundles(bundles map[string]*bundle.Bundle) *Runner

SetBundles will add bundles to the Runner which will be compiled then used for discovering and evaluating tests.

func (*Runner) SetCompiler Uses

func (r *Runner) SetCompiler(compiler *ast.Compiler) *Runner

SetCompiler sets the compiler used by the runner.

func (*Runner) SetCoverageTracer Uses

func (r *Runner) SetCoverageTracer(tracer topdown.Tracer) *Runner

SetCoverageTracer sets the tracer to use to compute coverage.

func (*Runner) SetModules Uses

func (r *Runner) SetModules(modules map[string]*ast.Module) *Runner

SetModules will add modules to the Runner which will be compiled then used for discovering and evaluating tests.

func (*Runner) SetRuntime Uses

func (r *Runner) SetRuntime(term *ast.Term) *Runner

SetRuntime sets runtime information to expose to the evaluation engine.

func (*Runner) SetStore Uses

func (r *Runner) SetStore(store storage.Store) *Runner

SetStore sets the store to execute tests over.

func (*Runner) SetTimeout Uses

func (r *Runner) SetTimeout(timout time.Duration) *Runner

SetTimeout sets the timeout for the individual test cases

Package tester imports 19 packages (graph) and is imported by 8 packages. Updated 2020-04-21. Refresh now. Tools for package owners.