{"id":13788091,"url":"https://github.com/anderseknert/rego-test-assertions","last_synced_at":"2026-02-17T01:32:59.336Z","repository":{"id":47843645,"uuid":"473971368","full_name":"anderseknert/rego-test-assertions","owner":"anderseknert","description":"Tiny Rego library with helper functions for unit testing","archived":false,"fork":false,"pushed_at":"2025-09-01T06:57:06.000Z","size":37,"stargazers_count":40,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-10-07T14:41:05.262Z","etag":null,"topics":["assertions","assertions-library","opa","open-policy-agent","policy","policy-as-code","rego","testing"],"latest_commit_sha":null,"homepage":"","language":"Open Policy Agent","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/anderseknert.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2022-03-25T10:49:19.000Z","updated_at":"2025-09-01T06:57:05.000Z","dependencies_parsed_at":"2023-01-24T01:45:45.569Z","dependency_job_id":"35cefa27-8244-4542-9161-1c1d132e6a23","html_url":"https://github.com/anderseknert/rego-test-assertions","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/anderseknert/rego-test-assertions","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anderseknert%2Frego-test-assertions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anderseknert%2Frego-test-assertions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anderseknert%2Frego-test-assertions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anderseknert%2Frego-test-assertions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anderseknert","download_url":"https://codeload.github.com/anderseknert/rego-test-assertions/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anderseknert%2Frego-test-assertions/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29529434,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-17T00:57:22.232Z","status":"ssl_error","status_checked_at":"2026-02-17T00:54:25.811Z","response_time":115,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["assertions","assertions-library","opa","open-policy-agent","policy","policy-as-code","rego","testing"],"created_at":"2024-08-03T21:00:36.698Z","updated_at":"2026-02-17T01:32:59.305Z","avatar_url":"https://github.com/anderseknert.png","language":"Open Policy Agent","funding_links":[],"categories":["Testing"],"sub_categories":["Serverless Blogs and Articles"],"readme":"# rego-test-assertions\n\nTiny [Rego](https://www.openpolicyagent.org/docs/latest/policy-language/) library providing helper\nfunctions for unit testing. The library primarily contains various assertion functions, which will\n[print](https://blog.openpolicyagent.org/introducing-the-opa-print-function-809da6a13aee)\nthe expected result vs. the outcome to the console on failure. This allows you to quickly grasp\nwhat went wrong in your unit tests, resulting in a faster test iteration process!\n\n\u003e This assert library is what I’ve been wanting since I started using OPA.\n\u003e Fixed something in ten minutes, while eating a sandwich, I’d previously been poking at for a couple of hours.\n\u003e You’ve made my day!\n\n— Duncan Thomas, Groupon\n\nNote that since OPA [v0.66.0](https://github.com/open-policy-agent/opa/releases/tag/v0.66.0), the `opa test` command\nhas built-in support for printing variable bindings for failed test assertions when run with the `--var-values` flag,\nwhich can be used as an alternative to this library if your goal is only to see what variable values were bound when\na test failed.\n\n## Usage\n\nSimply download the test assertions library\n[policy file](https://raw.githubusercontent.com/anderseknert/rego-test-assertions/main/test/assert.rego)\nand place it somewhere `opa test` checks for policy and tests:\n\nAlternatively, use [ODM](https://github.com/johanfylling/opa-dependency-manager) to add the library to your project:\n\n```shell\nodm depend rego-test-assertions --no-namespace \\\n    git+https://github.com/anderseknert/rego-test-assertions\n```\n\nIn order to use the test assertion functions, import the `test.assert` package:\n\n```rego\nimport data.test.assert\n```\n\n## Functions\n\nOnce imported, all functions may now be referenced like `assert.\u003cfunction\u003e`. Using the assert package prefix avoids\nhaving these functions clash with other built-ins and custom functions, and makes it clear in your test code what\nthe purpose of these functions is. As an added bonus, you won't need repeated import statements to import each\nfunction separetely.\n\n| Function                   | Arguments            | Example console output                                   |\n|----------------------------|----------------------|----------------------------------------------------------|\n| `assert.equals`            | `expected`, `result` | expected equals: \"foo\" got: \"bar\"                        |\n| `assert.not_equals`        | `expected`, `result` | expected not equals: 1 got: 1                            |\n| `assert.all_equals`        | `coll`, `value`      | expected all items to have value 2, failed for [1]       |\n| `assert.none_equals`       | `coll`, `value`      | expected no items to have value 2, failed for [2, 2]     |\n| `assert.has`               | `item`, `coll`       | expected string \"foo\" in array got: [\"bar\", \"baz\"]       |\n| `assert.not_has`           | `item`, `coll`       | expected string \"foo\" not in set got {\"foo\", \"x\"}        |\n| `assert.empty`             | `coll`               | expected empty set got: {\"admin\", \"dba\"}                 |\n| `assert.not_empty`         | `coll`               | expected empty array                                     |\n| `assert.starts_with`       | `str`, `search`      | expected \"test\" to start with \"a\"                        |\n| `assert.ends_with`         | `str`, `search`      | expected \"test\" to end with \"a\"                          |\n| `assert.all_starts_with`   | `coll`, `search`     | expected all strings to start with \"a\", failed for [\"b\"] |\n| `assert.all_ends_with`     | `coll`, `search`     | expected all strings to end with \"a\", failed for [\"b\"]   |\n| `assert.none_starts_swith` | `coll`, `search`     | expected no strings to start with \"a\", failed for [\"a\"]  |\n| `assert.none_ends_swith`   | `coll`, `search`     | expected no strings to end with \"a\", failed for [\"a\"]    |\n| `assert.fail`              | `msg`                | fail with provided message!                              |\n\n## Example\n\n### Policy\n\n```rego\npackage rego.example\n\ndeny[msg] {\n    msg := \"I'll always deny that\"\n}\n\nviolation[msg] {\n    input.user.name == \"banned\"\n    msg := \"You're banned!\"\n}\n```\n\n### Test without assertions\n\n```rego\npackage rego.example_test\n\nimport future.keywords.in\n\nimport data.example.deny\nimport data.example.violation\n\ntest_empty_without_assertion {\n    count(deny) == 0\n}\n\ntest_in_without_assertion {\n    \"You're banned!\" in violation with input.user.name as \"bob\"\n}\n```\n\n```shell\n❯ opa test example_test.rego\nexample_test.rego:\ndata.rego.example_test.test_empty_without_assertion: FAIL (178.375µs)\ndata.rego.example_test.test_in_without_assertion: FAIL (99.416µs)\n\n--------------------------------------------------------------------------------\nFAIL: 2/2\n```\n\n### Test with assertions\n\n```rego\npackage rego.example_test\n\nimport data.example.deny\nimport data.example.violation\n\nimport data.test.assert\n\ntest_empty_with_assertion {\n    assert.empty(deny)\n}\n\ntest_in_with_assertion {\n    assert.has(\"You're banned!\", violation) with input.user.name as \"bob\"\n}\n```\n\n```shell\n❯ opa test example_test.rego\nexample_test.rego:\ndata.rego.example_test.test_empty_with_assertion: FAIL (206.5µs)\n\n  expected empty set got {\"I'll always deny that\"}\n\ndata.rego.example_test.test_in_with_assertion: FAIL (138.792µs)\n\n  expected string \"You're banned!\" in set got set()\n\n--------------------------------------------------------------------------------\nFAIL: 2/2\n```\n\n## Prerequisites\n\nOPA v0.39.0 and above — a [bug](https://github.com/open-policy-agent/opa/issues/4489) in older versions caused\nthe `print` function to error in `else` blocks, which this library makes heavy use of.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanderseknert%2Frego-test-assertions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanderseknert%2Frego-test-assertions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanderseknert%2Frego-test-assertions/lists"}