{"id":13896368,"url":"https://github.com/catwell/cwtest","last_synced_at":"2025-04-13T21:32:41.131Z","repository":{"id":5559820,"uuid":"6764712","full_name":"catwell/cwtest","owner":"catwell","description":"Tiny Teal / Lua test helper.","archived":false,"fork":false,"pushed_at":"2024-12-30T17:03:12.000Z","size":50,"stargazers_count":22,"open_issues_count":1,"forks_count":6,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-10T11:17:30.598Z","etag":null,"topics":["lua","tap","test"],"latest_commit_sha":null,"homepage":"","language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/catwell.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","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}},"created_at":"2012-11-19T17:57:43.000Z","updated_at":"2024-12-31T08:16:50.000Z","dependencies_parsed_at":"2025-02-24T03:33:44.117Z","dependency_job_id":"24759bee-d0ad-43c6-ac7e-b1ff3a2465d9","html_url":"https://github.com/catwell/cwtest","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/catwell%2Fcwtest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/catwell%2Fcwtest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/catwell%2Fcwtest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/catwell%2Fcwtest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/catwell","download_url":"https://codeload.github.com/catwell/cwtest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248786225,"owners_count":21161419,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["lua","tap","test"],"created_at":"2024-08-06T18:02:52.064Z","updated_at":"2025-04-13T21:32:41.102Z","avatar_url":"https://github.com/catwell.png","language":"Lua","readme":"# cwtest\n\n![CI Status](https://github.com/catwell/cwtest/actions/workflows/ci.yml/badge.svg?branch=master)\n\n\n## Presentation\n\nThis is a small Teal / Lua test helper with no hard dependency.\n\n## Alternatives\n\n- [busted](http://olivinelabs.com/busted/)\n- [lua-TestMore](http://fperrad.github.com/lua-TestMore/)\n- [Unit Testing](http://lua-users.org/wiki/UnitTesting) page on the Lua Users' Wiki\n\n## Basic usage\n\n```lua\nlocal cwtest = require \"cwtest\"\n\nlocal T = cwtest.new() -- instantiate a test\n\nT:start(\"Multiplication\"); do -- start a test\n  T:eq( 2 * 3, 6 ) -- test equality\n  T:eq( 3 * 3 * 3, 27 )\n  T:eq( 3 * 4, 13 ) -- uh?\n  T:eq( 7 * 7, 49 )\nend; T:done() -- end a test\n\nT:start(\"Squares\"); do -- you can re-use T once done\n  for i = 1, 10 do\n    local x = 0\n    for _ = 1, i do x = x + i end\n    T:eq( i * i, x )\n  end\nend; T:done()\n\nT:exit()\n```\n\nOutput:\n\n```\nMultiplication ...x. FAILED (3 OK, 1 KO)\n\n[FAIL] my.test.lua line 8\n  expected: 13\n       got: 12\n\nSquares .......... OK (10 OK, 0 KO)\n```\n\n## Changes\n\n### 3.0\n\ncwtest version 3 was rewritten in [Teal](https://github.com/teal-language/tl). Teal compiles down to Lua so you can still use it in Lua. It has the same interface with one major caveat: `failures`, `successes` and `suite` are now method accessors, i.e. you must use `T:successes()` instead of `T.successes`.\n\n## Details\n\n### do/end block\n\nWrapping tests in a `do/end` block is not mandatory. You could simply write this:\n\n```lua\nT:start(\"stuff\")\nT:eq( 6*7, 42 )\nT:done()\n```\n\nThat being said, the `do/end` blocks with indentation help to separate your tests visually and keep your variables local, so this style is a good practice.\n\n### Return value of done()\n\n`done()` returns `true` if all tests have succeeded, `false` otherwise. Among other things this allows you to abort after a failed test suite:\n\n```lua\nT:start(\"stuff\"); do\n  T:eq(continue, true)\nend\nif not T:done() then T:exit() end\n```\n\n### exit()\n\n`exit()` leaves the interpreter, returning an error code of 1 if any call to `done()`  has failed, 0 otherwise. You do not have to call `exit()`, but doing so makes cwtest work with CI software.\n\n### Other tests\n\n- `eq` called on tables uses deep comparison.\n- `neq` is the opposite of `eq`.\n- `yes` and `no` test boolean propositions.\n- `seq` can be used to compare two lists without considering order.\n- `err` tests that an error is raised by a function.\n\n### Planning\n\nIf you pass a number as the second argument of `start()`, it will be taken as the number of tests in the test suite. If a different number of tests is run before `done()` is called, the test suite will fail.\n\n### Arguments of new()\n\nYou can pass an arguments table to `new()`. It supports the following keys:\n\n#### verbosity\n\nIf you set `verbosity`, it must be an integer between 0 and 2, the default is 0.\n\nVerbosity level 1 will print errors inline as soon as they happen, which may be useful to debug an error that makes your tests crash later on.\n\nVerbosity level 2 will print successes in full form as well as errors. You probably do not need this.\n\n#### tap\n\nYou can set `tap` to `true` to switch the output format to [TAP](https://testanything.org). The format used is actually nested tap: test suites are top level tests, and individual assertions are nested one level. For instance, the example from [Basic usage](#basic-usage) would result in:\n\n        ok 1 - Multiplication 1\n        ok 2 - Multiplication 2\n        not ok 3 - Multiplication 3\n        ok 4 - Multiplication 4\n        1..4\n    not ok 1 - Multiplication (3 OK, 1 KO)\n        ok 1 - Squares 1\n        ok 2 - Squares 2\n        ok 3 - Squares 3\n        ok 4 - Squares 4\n        ok 5 - Squares 5\n        ok 6 - Squares 6\n        ok 7 - Squares 7\n        ok 8 - Squares 8\n        ok 9 - Squares 9\n        ok 10 - Squares 10\n        1..10\n    ok 2 - Squares (10 OK, 0 KO)\n    1..2\n\nYou can also pass a number to plan how many test suites will be run. TAP will also leverage [planning](#planning). If you do both, planning 2 test suites with respectively 4 and 10 tests, the output will become:\n\n    1..2\n        1..4\n        ok 1 - Multiplication 1\n        ok 2 - Multiplication 2\n        not ok 3 - Multiplication 3\n        ok 4 - Multiplication 4\n    not ok 1 - Multiplication (3 OK, 1 KO, 4 total)\n        1..10\n        ok 1 - Squares 1\n        ok 2 - Squares 2\n        ok 3 - Squares 3\n        ok 4 - Squares 4\n        ok 5 - Squares 5\n        ok 6 - Squares 6\n        ok 7 - Squares 7\n        ok 8 - Squares 8\n        ok 9 - Squares 9\n        ok 10 - Squares 10\n    ok 2 - Squares (10 OK, 0 KO, 10 total)\n\n#### env\n\nYou can override the previous settings using environment variables `CWTEST_VERBOSITY` and `CWTEST_TAP`. If you want to disable this, set `env` to `false`. You can also set `env` to any string, which will then be used as the prefix instead of `CWTEST_`.\n\n### Custom tests\n\nYou can define your own tests by adding methods to `T` and calling\n`pass_` and `fail_` methods.\nYou can find an example of this\n[in fakeredis](https://github.com/catwell/cw-lua/blob/0503a0cbda94ac006485eb16daf55ceb030408da/fakeredis/fakeredis.test.lua#L7) and another one [in cwtest's meta-tests](https://github.com/catwell/cwtest/blob/727e8b0bb0058916966e4b7f14c37dc7779eb0c9/cwtest.test.lua#L26).\n\n## Copyright\n\n- Copyright (c) 2012-2013 Moodstocks SAS\n- Copyright (c) from 2014 Pierre Chapuis\n\nSome code adapted from [Penlight](https://github.com/lunarmodules/Penlight), released under the MIT license by Steve Donovan and David Manura.\n","funding_links":[],"categories":["Lua"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcatwell%2Fcwtest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcatwell%2Fcwtest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcatwell%2Fcwtest/lists"}