{"id":13469085,"url":"https://github.com/sstephenson/bats","last_synced_at":"2025-09-28T20:31:58.109Z","repository":{"id":2121779,"uuid":"3064355","full_name":"sstephenson/bats","owner":"sstephenson","description":"Bash Automated Testing System","archived":true,"fork":false,"pushed_at":"2019-09-27T17:21:43.000Z","size":116,"stargazers_count":7122,"open_issues_count":137,"forks_count":517,"subscribers_count":183,"default_branch":"master","last_synced_at":"2025-01-11T04:47:42.834Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Shell","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/sstephenson.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}},"created_at":"2011-12-28T18:40:33.000Z","updated_at":"2025-01-08T15:04:44.000Z","dependencies_parsed_at":"2022-07-14T08:09:00.014Z","dependency_job_id":null,"html_url":"https://github.com/sstephenson/bats","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/sstephenson%2Fbats","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sstephenson%2Fbats/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sstephenson%2Fbats/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sstephenson%2Fbats/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sstephenson","download_url":"https://codeload.github.com/sstephenson/bats/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234563123,"owners_count":18853056,"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":[],"created_at":"2024-07-31T15:01:25.969Z","updated_at":"2025-09-28T20:31:57.740Z","avatar_url":"https://github.com/sstephenson.png","language":"Shell","funding_links":[],"categories":["Shell","others","Productivity tools","Producers"],"sub_categories":["Chess :chess_pawn:","Bash"],"readme":"# Bats: Bash Automated Testing System\n\nBats is a [TAP](http://testanything.org)-compliant testing framework\nfor Bash. It provides a simple way to verify that the UNIX programs\nyou write behave as expected.\n\nA Bats test file is a Bash script with special syntax for defining\ntest cases. Under the hood, each test case is just a function with a\ndescription.\n\n```bash\n#!/usr/bin/env bats\n\n@test \"addition using bc\" {\n  result=\"$(echo 2+2 | bc)\"\n  [ \"$result\" -eq 4 ]\n}\n\n@test \"addition using dc\" {\n  result=\"$(echo 2 2+p | dc)\"\n  [ \"$result\" -eq 4 ]\n}\n```\n\nBats is most useful when testing software written in Bash, but you can\nuse it to test any UNIX program.\n\nTest cases consist of standard shell commands. Bats makes use of\nBash's `errexit` (`set -e`) option when running test cases. If every\ncommand in the test case exits with a `0` status code (success), the\ntest passes. In this way, each line is an assertion of truth.\n\n\n## Running tests\n\nTo run your tests, invoke the `bats` interpreter with a path to a test\nfile. The file's test cases are run sequentially and in isolation. If\nall the test cases pass, `bats` exits with a `0` status code. If there\nare any failures, `bats` exits with a `1` status code.\n\nWhen you run Bats from a terminal, you'll see output as each test is\nperformed, with a check-mark next to the test's name if it passes or\nan \"X\" if it fails.\n\n    $ bats addition.bats\n     ✓ addition using bc\n     ✓ addition using dc\n\n    2 tests, 0 failures\n\nIf Bats is not connected to a terminal—in other words, if you\nrun it from a continuous integration system, or redirect its output to\na file—the results are displayed in human-readable, machine-parsable\n[TAP format](http://testanything.org).\n\nYou can force TAP output from a terminal by invoking Bats with the\n`--tap` option.\n\n    $ bats --tap addition.bats\n    1..2\n    ok 1 addition using bc\n    ok 2 addition using dc\n\n### Test suites\n\nYou can invoke the `bats` interpreter with multiple test file\narguments, or with a path to a directory containing multiple `.bats`\nfiles. Bats will run each test file individually and aggregate the\nresults. If any test case fails, `bats` exits with a `1` status code.\n\n\n## Writing tests\n\nEach Bats test file is evaluated _n+1_ times, where _n_ is the number of\ntest cases in the file. The first run counts the number of test cases,\nthen iterates over the test cases and executes each one in its own\nprocess.\n\nFor more details about how Bats evaluates test files, see \n[Bats Evaluation Process](https://github.com/sstephenson/bats/wiki/Bats-Evaluation-Process)\non the wiki.\n\n### `run`: Test other commands\n\nMany Bats tests need to run a command and then make assertions about\nits exit status and output. Bats includes a `run` helper that invokes\nits arguments as a command, saves the exit status and output into\nspecial global variables, and then returns with a `0` status code so\nyou can continue to make assertions in your test case.\n\nFor example, let's say you're testing that the `foo` command, when\npassed a nonexistent filename, exits with a `1` status code and prints\nan error message.\n\n```bash\n@test \"invoking foo with a nonexistent file prints an error\" {\n  run foo nonexistent_filename\n  [ \"$status\" -eq 1 ]\n  [ \"$output\" = \"foo: no such file 'nonexistent_filename'\" ]\n}\n```\n\nThe `$status` variable contains the status code of the command, and\nthe `$output` variable contains the combined contents of the command's\nstandard output and standard error streams.\n\nA third special variable, the `$lines` array, is available for easily\naccessing individual lines of output. For example, if you want to test\nthat invoking `foo` without any arguments prints usage information on\nthe first line:\n\n```bash\n@test \"invoking foo without arguments prints usage\" {\n  run foo\n  [ \"$status\" -eq 1 ]\n  [ \"${lines[0]}\" = \"usage: foo \u003cfilename\u003e\" ]\n}\n```\n\n### `load`: Share common code\n\nYou may want to share common code across multiple test files. Bats\nincludes a convenient `load` command for sourcing a Bash source file\nrelative to the location of the current test file. For example, if you\nhave a Bats test in `test/foo.bats`, the command\n\n```bash\nload test_helper\n```\n\nwill source the script `test/test_helper.bash` in your test file. This\ncan be useful for sharing functions to set up your environment or load\nfixtures.\n\n### `skip`: Easily skip tests\n\nTests can be skipped by using the `skip` command at the point in a\ntest you wish to skip.\n\n```bash\n@test \"A test I don't want to execute for now\" {\n  skip\n  run foo\n  [ \"$status\" -eq 0 ]\n}\n```\n\nOptionally, you may include a reason for skipping:\n\n```bash\n@test \"A test I don't want to execute for now\" {\n  skip \"This command will return zero soon, but not now\"\n  run foo\n  [ \"$status\" -eq 0 ]\n}\n```\n\nOr you can skip conditionally:\n\n```bash\n@test \"A test which should run\" {\n  if [ foo != bar ]; then\n    skip \"foo isn't bar\"\n  fi\n\n  run foo\n  [ \"$status\" -eq 0 ]\n}\n```\n\n### `setup` and `teardown`: Pre- and post-test hooks\n\nYou can define special `setup` and `teardown` functions, which run\nbefore and after each test case, respectively. Use these to load\nfixtures, set up your environment, and clean up when you're done.\n\n### Code outside of test cases\n\nYou can include code in your test file outside of `@test` functions.\nFor example, this may be useful if you want to check for dependencies\nand fail immediately if they're not present. However, any output that\nyou print in code outside of `@test`, `setup` or `teardown` functions\nmust be redirected to `stderr` (`\u003e\u00262`). Otherwise, the output may\ncause Bats to fail by polluting the TAP stream on `stdout`.\n\n### Special variables\n\nThere are several global variables you can use to introspect on Bats\ntests:\n\n* `$BATS_TEST_FILENAME` is the fully expanded path to the Bats test\nfile.\n* `$BATS_TEST_DIRNAME` is the directory in which the Bats test file is\nlocated.\n* `$BATS_TEST_NAMES` is an array of function names for each test case.\n* `$BATS_TEST_NAME` is the name of the function containing the current\ntest case.\n* `$BATS_TEST_DESCRIPTION` is the description of the current test\ncase.\n* `$BATS_TEST_NUMBER` is the (1-based) index of the current test case\nin the test file.\n* `$BATS_TMPDIR` is the location to a directory that may be used to\nstore temporary files.\n\n\n## Installing Bats from source\n\nCheck out a copy of the Bats repository. Then, either add the Bats\n`bin` directory to your `$PATH`, or run the provided `install.sh`\ncommand with the location to the prefix in which you want to install\nBats. For example, to install Bats into `/usr/local`,\n\n    $ git clone https://github.com/sstephenson/bats.git\n    $ cd bats\n    $ ./install.sh /usr/local\n\nNote that you may need to run `install.sh` with `sudo` if you do not\nhave permission to write to the installation prefix.\n\n\n## Support\n\nThe Bats source code repository is [hosted on\nGitHub](https://github.com/sstephenson/bats). There you can file bugs\non the issue tracker or submit tested pull requests for review.\n\nFor real-world examples from open-source projects using Bats, see\n[Projects Using Bats](https://github.com/sstephenson/bats/wiki/Projects-Using-Bats)\non the wiki.\n\nTo learn how to set up your editor for Bats syntax highlighting, see\n[Syntax Highlighting](https://github.com/sstephenson/bats/wiki/Syntax-Highlighting)\non the wiki.\n\n\n## Version history\n\n*0.4.0* (August 13, 2014)\n\n* Improved the display of failing test cases. Bats now shows the\n  source code of failing test lines, along with full stack traces\n  including function names, filenames, and line numbers.\n* Improved the display of the pretty-printed test summary line to\n  include the number of skipped tests, if any.\n* Improved the speed of the preprocessor, dramatically shortening test\n  and suite startup times.\n* Added support for absolute pathnames to the `load` helper.\n* Added support for single-line `@test` definitions.\n* Added bats(1) and bats(7) manual pages.\n* Modified the `bats` command to default to TAP output when the `$CI`\n  variable is set, to better support environments such as Travis CI.\n\n*0.3.1* (October 28, 2013)\n\n* Fixed an incompatibility with the pretty formatter in certain\n  environments such as tmux.\n* Fixed a bug where the pretty formatter would crash if the first line\n  of a test file's output was invalid TAP.\n\n*0.3.0* (October 21, 2013)\n\n* Improved formatting for tests run from a terminal. Failing tests\n  are now colored in red, and the total number of failing tests is\n  displayed at the end of the test run. When Bats is not connected to\n  a terminal (e.g. in CI runs), or when invoked with the `--tap` flag,\n  output is displayed in standard TAP format.\n* Added the ability to skip tests using the `skip` command.\n* Added a message to failing test case output indicating the file and\n  line number of the statement that caused the test to fail.\n* Added \"ad-hoc\" test suite support. You can now invoke `bats` with\n  multiple filename or directory arguments to run all the specified\n  tests in aggregate.\n* Added support for test files with Windows line endings.\n* Fixed regular expression warnings from certain versions of Bash.\n* Fixed a bug running tests containing lines that begin with `-e`.\n\n*0.2.0* (November 16, 2012)\n\n* Added test suite support. The `bats` command accepts a directory\n  name containing multiple test files to be run in aggregate.\n* Added the ability to count the number of test cases in a file or\n  suite by passing the `-c` flag to `bats`.\n* Preprocessed sources are cached between test case runs in the same\n  file for better performance.\n\n*0.1.0* (December 30, 2011)\n\n* Initial public release.\n\n---\n\n© 2014 Sam Stephenson. Bats is released under an MIT-style license;\nsee `LICENSE` for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsstephenson%2Fbats","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsstephenson%2Fbats","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsstephenson%2Fbats/lists"}