Module lester
48 | Lester is a minimal unit testing framework for Lua with a focus on being simple to use.
49 |50 | 51 | 52 |
Features
53 | 54 |-
55 |
- Minimal, just one file. 56 |
- Self contained, no external dependencies. 57 |
- Simple and hackable when needed. 58 |
- Use describe and it blocks to describe tests. 59 |
- Supports before and after handlers. 60 |
- Colored output. 61 |
- Configurable via the script or with environment variables. 62 |
- Quiet mode, to use in live development. 63 |
- Optionally filter tests by name. 64 |
- Show traceback on errors. 65 |
- Show time to complete tests. 66 |
- Works with Lua 5.1+. 67 |
- Efficient. 68 |
Usage
71 | 72 |Copy lester.lua
file to a project and require it,
73 | which returns a table that includes all of the functionality:
77 | local lester = require 'lester' 78 | local describe, it, expect = lester.describe, lester.it, lester.expect 79 | 80 | -- Customize lester configuration. 81 | lester.show_traceback = false 82 | 83 | -- Parse arguments from command line. 84 | lester.parse_args() 85 | 86 | describe('my project', function() 87 | lester.before(function() 88 | -- This function is run before every test. 89 | end) 90 | 91 | describe('module1', function() -- Describe blocks can be nested. 92 | it('feature1', function() 93 | expect.equal('something', 'something') -- Pass. 94 | end) 95 | 96 | it('feature2', function() 97 | expect.truthy(false) -- Fail. 98 | end) 99 | 100 | local feature3_test_enabled = false 101 | it('feature3', function() -- This test will be skipped. 102 | expect.truthy(false) -- Fail. 103 | end, feature3_test_enabled) 104 | end) 105 | end) 106 | 107 | lester.report() -- Print overall statistic of the tests run. 108 | lester.exit() -- Exit with success if all tests passed. 109 |110 | 111 | 112 |
Customizing output with environment variables
113 | 114 |To customize the output of lester externally, 115 | you can set the following environment variables before running a test suite:
116 | 117 |-
118 |
LESTER_QUIET="true"
, omit print of passed tests.
119 | LESTER_COLOR="false"
, disable colored output.
120 | LESTER_SHOW_TRACEBACK="false"
, disable traceback on test failures.
121 | LESTER_SHOW_ERROR="false"
, omit print of error description of failed tests.
122 | LESTER_STOP_ON_FAIL="true"
, stop on first test failure.
123 | LESTER_UTF8TERM="false"
, disable printing of UTF-8 characters.
124 | LESTER_FILTER="some text"
, filter the tests that should be run.
125 |
Note that these configurations can be changed via script too, check the documentation.
128 | 129 |Customizing output with command line arguments
130 | 131 |You can also customize output using command line arguments
132 | if lester.parse_args()
is called at startup.
The following command line arguments are available:
135 | 136 |-
137 |
--quiet
, omit print of passed tests.
138 | --no-quiet
, show print of passed tests.
139 | --no-color
, disable colored output.
140 | --no-show-traceback
, disable traceback on test failures.
141 | --no-show-error
, omit print of error description of failed tests.
142 | --stop-on-fail
, stop on first test failure.
143 | --no-utf8term
, disable printing of UTF-8 characters.
144 | --filter="some text"
, filter the tests that should be run.
145 |
Functions
150 |describe (name, func) | 153 |Describe a block of tests, which consists in a set of tests. | 154 |
it (name, func, enabled) | 157 |Declare a test, which consists of a set of assertions. | 158 |
before (func) | 161 |Set a function that is called before every test inside a describe block. | 162 |
after (func) | 165 |Set a function that is called after every test inside a describe block. | 166 |
report () | 169 |Pretty print statistics of all test runs. | 170 |
exit () | 173 |Exit the application with success code if all tests passed, or failure code otherwise. | 174 |
expect.tohumanstring (v) | 177 |Converts a value to a human-readable string. | 178 |
expect.fail (func, expected) | 181 |Check if a function fails with an error. | 182 |
expect.not_fail (func) | 185 |Check if a function does not fail with a error. | 186 |
expect.exist (v) | 189 |Check if a value is not nil . |
190 |
expect.not_exist (v) | 193 |Check if a value is nil . |
194 |
expect.truthy (v) | 197 |Check if an expression is evaluates to true . |
198 |
expect.falsy (v) | 201 |Check if an expression is evaluates to false . |
202 |
expect.strict_eq (t1, t2, name) | 205 |Compare if two values are equal, considering nested tables. | 206 |
expect.equal (v1, v2) | 209 |Check if two values are equal. | 210 |
expect.not_equal (v1, v2) | 213 |Check if two values are not equal. | 214 |
Fields
217 |quiet | 220 |Whether lines of passed tests should not be printed. | 221 |
color | 224 |Whether the output should be colorized. | 225 |
show_traceback | 228 |Whether a traceback must be shown on test failures. | 229 |
show_error | 232 |Whether the error description of a test failure should be shown. | 233 |
stop_on_fail | 236 |Whether test suite should exit on first test failure. | 237 |
utf8term | 240 |Whether we can print UTF-8 characters to the terminal. | 241 |
filter | 244 |A string with a lua pattern to filter tests. | 245 |
seconds | 248 |Function to retrieve time in seconds with milliseconds precision, os.clock by default. | 249 |
colors | 252 |Table of terminal colors codes, can be customized. | 253 |
expect | 256 |Expect module, containing utility function for doing assertions inside a test. | 257 |
261 |
262 | 263 | 264 |
Functions
265 | 266 |-
267 |
- 268 | 269 | describe (name, func) 270 | 271 |
-
272 | Describe a block of tests, which consists in a set of tests.
273 | Describes can be nested.
274 |
275 |
276 |
Parameters:
277 |-
278 |
- name 279 | A string used to describe the block. 280 | 281 |
- func 282 | A function containing all the tests or other describes. 283 | 284 |
291 | - 292 | 293 | it (name, func, enabled) 294 | 295 |
-
296 | Declare a test, which consists of a set of assertions.
297 |
298 |
299 |
Parameters:
300 |-
301 |
- name 302 | A name for the test. 303 | 304 |
- func 305 | The function containing all assertions. 306 | 307 |
- enabled 308 | If not nil and equals to false, the test will be skipped and this will be reported. 309 | 310 |
317 | - 318 | 319 | before (func) 320 | 321 |
-
322 | Set a function that is called before every test inside a describe block.
323 | A single string containing the name of the test about to be run will be passed to
func
. 324 | 325 | 326 |Parameters:
327 |-
328 |
- func 329 | 330 | 331 | 332 | 333 |
340 | - 341 | 342 | after (func) 343 | 344 |
-
345 | Set a function that is called after every test inside a describe block.
346 | A single string containing the name of the test that was finished will be passed to
func
. 347 | The function is executed independently if the test passed or failed. 348 | 349 | 350 |Parameters:
351 |-
352 |
- func 353 | 354 | 355 | 356 | 357 |
364 | - 365 | 366 | report () 367 | 368 |
- 369 | Pretty print statistics of all test runs. 370 | With total success, total failures and run time in seconds. 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 |
- 380 | 381 | exit () 382 | 383 |
- 384 | Exit the application with success code if all tests passed, or failure code otherwise. 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 |
- 394 | 395 | expect.tohumanstring (v) 396 | 397 |
-
398 | Converts a value to a human-readable string.
399 | If the final string not contains only ASCII characters,
400 | then it is converted to a Lua hexdecimal string.
401 |
402 |
403 |
Parameters:
404 |-
405 |
- v 406 | 407 | 408 | 409 | 410 |
417 | - 418 | 419 | expect.fail (func, expected) 420 | 421 |
-
422 | Check if a function fails with an error.
423 | If
expected
is nil then any error is accepted. 424 | Ifexpected
is a string then we check if the error contains that string. 425 | Ifexpected
is anything else then we check if both are equal. 426 | 427 | 428 |Parameters:
429 |-
430 |
- func 431 | 432 | 433 | 434 | 435 |
- expected 436 | 437 | 438 | 439 | 440 |
447 | - 448 | 449 | expect.not_fail (func) 450 | 451 |
-
452 | Check if a function does not fail with a error.
453 |
454 |
455 |
Parameters:
456 |-
457 |
- func 458 | 459 | 460 | 461 | 462 |
469 | - 470 | 471 | expect.exist (v) 472 | 473 |
-
474 | Check if a value is not
nil
. 475 | 476 | 477 |Parameters:
478 |-
479 |
- v 480 | 481 | 482 | 483 | 484 |
491 | - 492 | 493 | expect.not_exist (v) 494 | 495 |
-
496 | Check if a value is
nil
. 497 | 498 | 499 |Parameters:
500 |-
501 |
- v 502 | 503 | 504 | 505 | 506 |
513 | - 514 | 515 | expect.truthy (v) 516 | 517 |
-
518 | Check if an expression is evaluates to
true
. 519 | 520 | 521 |Parameters:
522 |-
523 |
- v 524 | 525 | 526 | 527 | 528 |
535 | - 536 | 537 | expect.falsy (v) 538 | 539 |
-
540 | Check if an expression is evaluates to
false
. 541 | 542 | 543 |Parameters:
544 |-
545 |
- v 546 | 547 | 548 | 549 | 550 |
557 | - 558 | 559 | expect.strict_eq (t1, t2, name) 560 | 561 |
-
562 | Compare if two values are equal, considering nested tables.
563 |
564 |
565 |
Parameters:
566 |-
567 |
- t1 568 | 569 | 570 | 571 | 572 |
- t2 573 | 574 | 575 | 576 | 577 |
- name 578 | 579 | 580 | 581 | 582 |
589 | - 590 | 591 | expect.equal (v1, v2) 592 | 593 |
-
594 | Check if two values are equal.
595 |
596 |
597 |
Parameters:
598 |-
599 |
- v1 600 | 601 | 602 | 603 | 604 |
- v2 605 | 606 | 607 | 608 | 609 |
616 | - 617 | 618 | expect.not_equal (v1, v2) 619 | 620 |
-
621 | Check if two values are not equal.
622 |
623 |
624 |
Parameters:
625 |-
626 |
- v1 627 | 628 | 629 | 630 | 631 |
- v2 632 | 633 | 634 | 635 | 636 |
643 |
Fields
645 | 646 |-
647 |
- 648 | 649 | quiet 650 | 651 |
- 652 | Whether lines of passed tests should not be printed. False by default. 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 |
- 662 | 663 | color 664 | 665 |
- 666 | Whether the output should be colorized. True by default. 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 675 |
- 676 | 677 | show_traceback 678 | 679 |
- 680 | Whether a traceback must be shown on test failures. True by default. 681 | 682 | 683 | 684 | 685 | 686 | 687 | 688 | 689 |
- 690 | 691 | show_error 692 | 693 |
- 694 | Whether the error description of a test failure should be shown. True by default. 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 |
- 704 | 705 | stop_on_fail 706 | 707 |
- 708 | Whether test suite should exit on first test failure. False by default. 709 | 710 | 711 | 712 | 713 | 714 | 715 | 716 | 717 |
- 718 | 719 | utf8term 720 | 721 |
- 722 | Whether we can print UTF-8 characters to the terminal. True by default when supported. 723 | 724 | 725 | 726 | 727 | 728 | 729 | 730 | 731 |
- 732 | 733 | filter 734 | 735 |
- 736 | A string with a lua pattern to filter tests. Nil by default. 737 | 738 | 739 | 740 | 741 | 742 | 743 | 744 | 745 |
- 746 | 747 | seconds 748 | 749 |
- 750 | Function to retrieve time in seconds with milliseconds precision, os.clock by default. 751 | 752 | 753 | 754 | 755 | 756 | 757 | 758 | 759 |
- 760 | 761 | colors 762 | 763 |
- 764 | Table of terminal colors codes, can be customized. 765 | 766 | 767 | 768 | 769 | 770 | 771 | 772 | 773 |
- 774 | 775 | expect 776 | 777 |
- 778 | Expect module, containing utility function for doing assertions inside a test. 779 | 780 | 781 | 782 | 783 | 784 | 785 | 786 | 787 |