1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome to LuaUnit’s documentation! — LuaUnit 1.6 documentation</title>
<link rel="stylesheet" href="_static/default.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '1.6',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="LuaUnit 1.6 documentation" href="#" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li><a href="#">LuaUnit 1.6 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="welcome-to-luaunit-s-documentation">
<h1>Welcome to LuaUnit’s documentation!<a class="headerlink" href="#welcome-to-luaunit-s-documentation" title="Permalink to this headline">¶</a></h1>
<div class="toctree-wrapper compound">
<ul class="simple">
</ul>
</div>
<div class="section" id="introduction">
<h2>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
<p>Luaunit is a unit-testing framework for Lua. It allows you
to write test functions and test classes with test methods, combined with
setup/teardown functionality. A wide range of assertions are supported.</p>
<p>Luaunit supports several output format, like Junit or TAP, for easier integration
into Continuous Integration platforms (Jenkins, Maven, ...) . The integrated command-line
options provide a flexible interface to select tests by name or patterns, control output
format, set verbosity, ...</p>
<div class="section" id="platform-support">
<h3>Platform support<a class="headerlink" href="#platform-support" title="Permalink to this headline">¶</a></h3>
<p>LuaUnit works with Lua 5.1 and 5.2 . It is tested on Windows XP and Ubuntu 12.04 (see
continuous build results on <a class="reference external" href="https://travis-ci.org/bluebird75/luaunit">Travis-CI</a> ) and should work on all platforms supported by lua.
It has no other dependency than lua itself.</p>
<p>LuaUnit is packed into a single-file. To make start using it, just add the file to your project.</p>
</div>
<div class="section" id="development">
<h3>Development<a class="headerlink" href="#development" title="Permalink to this headline">¶</a></h3>
<p>Luaunit is developed on <a class="reference external" href="https://github.com/bluebird75/luaunit">Github</a>.</p>
<p>Bugs or feature requests must be reported using <a class="reference external" href="https://github.com/bluebird75/luaunit/issues">GitHub issues</a>.</p>
<p>It is released under the BSD license.</p>
<p>This documentation is available at <a class="reference external" href="http://luaunit.readthedocs.org/en/latest/">Read-the-docs</a>.</p>
</div>
</div>
<div class="section" id="getting-started">
<h2>Getting started<a class="headerlink" href="#getting-started" title="Permalink to this headline">¶</a></h2>
<div class="section" id="setting-up-your-test-script">
<h3>Setting up your test script<a class="headerlink" href="#setting-up-your-test-script" title="Permalink to this headline">¶</a></h3>
<p>To get started, create your file <em>test_something.lua</em> .</p>
<p>The script should import LuaUnit:</p>
<div class="highlight-lua"><div class="highlight"><pre><span class="nb">require</span><span class="p">(</span><span class="s1">'</span><span class="s">luaunit'</span><span class="p">)</span>
</pre></div>
</div>
<p>The last line executes your script with LuaUnit and exit with the
proper error code:</p>
<div class="highlight-lua"><div class="highlight"><pre><span class="nb">os.exit</span><span class="p">(</span> <span class="n">LuaUnit</span><span class="p">.</span><span class="n">run</span><span class="p">()</span> <span class="p">)</span>
</pre></div>
</div>
<p>Now, run your file with:</p>
<div class="highlight-lua"><div class="highlight"><pre><span class="n">lua</span> <span class="n">test_something</span><span class="p">.</span><span class="n">lua</span>
</pre></div>
</div>
<p>It prints something like:</p>
<div class="highlight-lua"><div class="highlight"><pre><span class="n">Started</span> <span class="n">on</span> <span class="mi">08</span><span class="o">/</span><span class="mi">07</span><span class="o">/</span><span class="mi">14</span> <span class="mi">07</span><span class="p">:</span><span class="mi">07</span><span class="p">:</span><span class="mi">40</span>
<span class="o">=========================================================</span>
<span class="n">Success</span><span class="p">:</span> <span class="mi">100</span><span class="o">%</span> <span class="o">-</span> <span class="mi">0</span> <span class="o">/</span> <span class="mi">0</span><span class="p">,</span> <span class="n">executed</span> <span class="k">in</span> <span class="mf">0.000</span> <span class="n">seconds</span>
</pre></div>
</div>
<p>Now, your testing framework is in place, you can start writing tests.</p>
</div>
<div class="section" id="writing-tests">
<h3>Writing tests<a class="headerlink" href="#writing-tests" title="Permalink to this headline">¶</a></h3>
<p>LuaUnit scans all variables that start with <em>test</em> or <em>Test</em>.
If they are functions, or if they are tables that contain
functions that start with <em>test</em> or <em>Test</em>, they are run as part of the test suite.</p>
<p>So just write a function whose name starts with test. Inside test functions, use the assertions functions provided by LuaUnit, such
as <a class="reference internal" href="#assertEquals" title="assertEquals"><tt class="xref py py-func docutils literal"><span class="pre">assertEquals()</span></tt></a>.</p>
<p>Let’s see that in practice.</p>
<p>Suppose you want to test the following add function:</p>
<div class="highlight-lua"><div class="highlight"><pre><span class="k">function</span> <span class="nf">add</span><span class="p">(</span><span class="n">v1</span><span class="p">,</span><span class="n">v2</span><span class="p">)</span>
<span class="c1">-- add positive numbers</span>
<span class="c1">-- return 0 if any of the numbers are 0</span>
<span class="c1">-- error if any of the two numbers are negative</span>
<span class="k">if</span> <span class="n">v1</span> <span class="o"><</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">v2</span> <span class="o"><</span> <span class="mi">0</span> <span class="k">then</span>
<span class="nb">error</span><span class="p">(</span><span class="s1">'</span><span class="s">Can only add positive or null numbers, received '</span><span class="o">..</span><span class="n">v1</span><span class="o">..</span><span class="s1">'</span><span class="s"> and '</span><span class="o">..</span><span class="n">v2</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">if</span> <span class="n">v1</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">v2</span> <span class="o">==</span> <span class="mi">0</span> <span class="k">then</span>
<span class="k">return</span> <span class="mi">0</span>
<span class="k">end</span>
<span class="k">return</span> <span class="n">v1</span><span class="o">+</span><span class="n">v2</span>
<span class="k">end</span>
</pre></div>
</div>
<p>You write the following tests:</p>
<div class="highlight-lua"><div class="highlight"><pre><span class="k">function</span> <span class="nf">testAddPositive</span><span class="p">()</span>
<span class="n">assertEquals</span><span class="p">(</span><span class="n">add</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">),</span><span class="mi">2</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">function</span> <span class="nf">testAddZero</span><span class="p">()</span>
<span class="n">assertEquals</span><span class="p">(</span><span class="n">add</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">),</span><span class="mi">0</span><span class="p">)</span>
<span class="n">assertEquals</span><span class="p">(</span><span class="n">add</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">5</span><span class="p">),</span><span class="mi">0</span><span class="p">)</span>
<span class="n">assertEquals</span><span class="p">(</span><span class="n">add</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">),</span><span class="mi">0</span><span class="p">)</span>
<span class="k">end</span>
</pre></div>
</div>
<p><a class="reference internal" href="#assertEquals" title="assertEquals"><tt class="xref py py-func docutils literal"><span class="pre">assertEquals()</span></tt></a> is the most common used assertion function. It simply
verifies that both argument are equals, in the order actual value, expected value.</p>
<p>Rerun your test script:</p>
<div class="highlight-lua"><div class="highlight"><pre><span class="n">lua</span> <span class="n">test_something</span><span class="p">.</span><span class="n">lua</span>
</pre></div>
</div>
<p>It now prints:</p>
<div class="highlight-lua"><div class="highlight"><pre><span class="n">Started</span> <span class="n">on</span> <span class="mi">08</span><span class="o">/</span><span class="mi">16</span><span class="o">/</span><span class="mi">14</span> <span class="mi">13</span><span class="p">:</span><span class="mi">55</span><span class="p">:</span><span class="mi">18</span>
<span class="o">>>>>>>>>></span> <span class="o"><</span><span class="n">TestFunctions</span><span class="o">></span>
<span class="o">>>></span> <span class="n">testAddPositive</span>
<span class="o">>>></span> <span class="n">testAddZero</span>
<span class="o">=========================================================</span>
<span class="n">Success</span><span class="p">:</span> <span class="mi">100</span><span class="o">%</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">/</span> <span class="mi">2</span><span class="p">,</span> <span class="n">executed</span> <span class="k">in</span> <span class="mf">0.000</span> <span class="n">seconds</span>
</pre></div>
</div>
<p>You always have:</p>
<ul class="simple">
<li>the date at which the test suite was started</li>
<li>the group to which the function belongs (usually, the name of the function table, and <em><TestFunctions></em> for all direct test functions)</li>
<li>the name of the function being executed</li>
<li>a report at the end, with number of executed test, success rate and execution duration</li>
</ul>
<p>You also want to test that when the function receives negative numbers, it generates an error. Use
<a class="reference internal" href="#assertError" title="assertError"><tt class="xref py py-func docutils literal"><span class="pre">assertError()</span></tt></a> or even better, <a class="reference internal" href="#assertErrorMsgContains" title="assertErrorMsgContains"><tt class="xref py py-func docutils literal"><span class="pre">assertErrorMsgContains()</span></tt></a> to also validate the content
of the error message. There are other types or error checking functions, see <a class="reference internal" href="#assert-error"><em>Error assertions</em></a> . Here
we use <a class="reference internal" href="#assertErrorMsgContains" title="assertErrorMsgContains"><tt class="xref py py-func docutils literal"><span class="pre">assertErrorMsgContains()</span></tt></a> . First argument is the expected message, then the function to call
and the optional arguments:</p>
<div class="highlight-lua"><div class="highlight"><pre><span class="k">function</span> <span class="nf">testAddError</span><span class="p">()</span>
<span class="n">assertErrorMsgContains</span><span class="p">(</span><span class="s1">'</span><span class="s">Can only add positive or null numbers, received 2 and -3'</span><span class="p">,</span> <span class="n">add</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">)</span>
<span class="k">end</span>
</pre></div>
</div>
<p>Now, suppose we also have the following function to test:</p>
<div class="highlight-lua"><div class="highlight"><pre><span class="k">function</span> <span class="nf">adder</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
<span class="c1">-- return a function that adds v to its argument using add</span>
<span class="k">function</span> <span class="nf">closure</span><span class="p">(</span> <span class="n">x</span> <span class="p">)</span> <span class="k">return</span> <span class="n">x</span><span class="o">+</span><span class="n">v</span> <span class="k">end</span>
<span class="k">return</span> <span class="n">closure</span>
<span class="k">end</span>
</pre></div>
</div>
<p>We want to test the type of the value returned by adder and its behavior. LuaUnit
provides assertion for type testing (see <a class="reference internal" href="#assert-type"><em>Type assertions</em></a>). In this case, we use
<a class="reference internal" href="#assertIsFunction" title="assertIsFunction"><tt class="xref py py-func docutils literal"><span class="pre">assertIsFunction()</span></tt></a>:</p>
<div class="highlight-lua"><div class="highlight"><pre><span class="k">function</span> <span class="nf">testAdder</span><span class="p">()</span>
<span class="n">f</span> <span class="o">=</span> <span class="n">adder</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="n">assertIsFunction</span><span class="p">(</span> <span class="n">f</span> <span class="p">)</span>
<span class="n">assertEquals</span><span class="p">(</span> <span class="n">f</span><span class="p">(</span><span class="mi">2</span><span class="p">),</span> <span class="mi">5</span> <span class="p">)</span>
<span class="k">end</span>
</pre></div>
</div>
</div>
<div class="section" id="grouping-tests-setup-teardown-functionality">
<h3>Grouping tests, setup/teardown functionality<a class="headerlink" href="#grouping-tests-setup-teardown-functionality" title="Permalink to this headline">¶</a></h3>
<p>When the number of tests starts to grow, you usually organise them
into separate groups. You can do that with LuaUnit by putting them
inside a table (whose name must start with <em>Test</em> or <em>test</em> ).</p>
<p>For example, assume we have a second function to test:</p>
<div class="highlight-lua"><div class="highlight"><pre><span class="k">function</span> <span class="nf">div</span><span class="p">(</span><span class="n">v1</span><span class="p">,</span><span class="n">v2</span><span class="p">)</span>
<span class="c1">-- divide positive numbers</span>
<span class="c1">-- return 0 if any of the numbers are 0</span>
<span class="c1">-- error if any of the two numbers are negative</span>
<span class="k">if</span> <span class="n">v1</span> <span class="o"><</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">v2</span> <span class="o"><</span> <span class="mi">0</span> <span class="k">then</span>
<span class="nb">error</span><span class="p">(</span><span class="s1">'</span><span class="s">Can only divide positive or null numbers, received '</span><span class="o">..</span><span class="n">v1</span><span class="o">..</span><span class="s1">'</span><span class="s"> and '</span><span class="o">..</span><span class="n">v2</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">if</span> <span class="n">v1</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">v2</span> <span class="o">==</span> <span class="mi">0</span> <span class="k">then</span>
<span class="k">return</span> <span class="mi">0</span>
<span class="k">end</span>
<span class="k">return</span> <span class="n">v1</span><span class="o">/</span><span class="n">v2</span>
<span class="k">end</span>
</pre></div>
</div>
<p>We move the tests related to the function add into their own table:</p>
<div class="highlight-lua"><div class="highlight"><pre><span class="n">TestAdd</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">function</span> <span class="nf">TestAdd</span><span class="p">:</span><span class="n">testAddPositive</span><span class="p">()</span>
<span class="n">assertEquals</span><span class="p">(</span><span class="n">add</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">),</span><span class="mi">2</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">function</span> <span class="nf">TestAdd</span><span class="p">:</span><span class="n">testAddZero</span><span class="p">()</span>
<span class="n">assertEquals</span><span class="p">(</span><span class="n">add</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">),</span><span class="mi">0</span><span class="p">)</span>
<span class="n">assertEquals</span><span class="p">(</span><span class="n">add</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">5</span><span class="p">),</span><span class="mi">0</span><span class="p">)</span>
<span class="n">assertEquals</span><span class="p">(</span><span class="n">add</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">),</span><span class="mi">0</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">function</span> <span class="nf">TestAdd</span><span class="p">:</span><span class="n">testAddError</span><span class="p">()</span>
<span class="n">assertErrorMsgContains</span><span class="p">(</span><span class="s1">'</span><span class="s">Can only add positive or null numbers, received 2 and -3'</span><span class="p">,</span> <span class="n">add</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">function</span> <span class="nf">TestAdd</span><span class="p">:</span><span class="n">testAdder</span><span class="p">()</span>
<span class="n">f</span> <span class="o">=</span> <span class="n">adder</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="n">assertIsFunction</span><span class="p">(</span> <span class="n">f</span> <span class="p">)</span>
<span class="n">assertEquals</span><span class="p">(</span> <span class="n">f</span><span class="p">(</span><span class="mi">2</span><span class="p">),</span> <span class="mi">5</span> <span class="p">)</span>
<span class="k">end</span>
<span class="c1">-- end of table TestAdd</span>
</pre></div>
</div>
<p>Then we create a second set of tests for div:</p>
<div class="highlight-lua"><div class="highlight"><pre><span class="n">TestDiv</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">function</span> <span class="nf">TestDiv</span><span class="p">:</span><span class="n">testDivPositive</span><span class="p">()</span>
<span class="n">assertEquals</span><span class="p">(</span><span class="n">div</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span><span class="mi">2</span><span class="p">),</span><span class="mi">2</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">function</span> <span class="nf">TestDiv</span><span class="p">:</span><span class="n">testDivZero</span><span class="p">()</span>
<span class="n">assertEquals</span><span class="p">(</span><span class="n">div</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span><span class="mi">0</span><span class="p">),</span><span class="mi">0</span><span class="p">)</span>
<span class="n">assertEquals</span><span class="p">(</span><span class="n">div</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">5</span><span class="p">),</span><span class="mi">0</span><span class="p">)</span>
<span class="n">assertEquals</span><span class="p">(</span><span class="n">div</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">),</span><span class="mi">0</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">function</span> <span class="nf">TestDiv</span><span class="p">:</span><span class="n">testDivError</span><span class="p">()</span>
<span class="n">assertErrorMsgContains</span><span class="p">(</span><span class="s1">'</span><span class="s">Can only div positive or null numbers, received 2 and -3'</span><span class="p">,</span> <span class="n">div</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">)</span>
<span class="k">end</span>
<span class="c1">-- end of table TestDiv</span>
</pre></div>
</div>
<p>Execution of the test suite now looks like this:</p>
<div class="highlight-lua"><div class="highlight"><pre><span class="n">Started</span> <span class="n">on</span> <span class="mi">08</span><span class="o">/</span><span class="mi">16</span><span class="o">/</span><span class="mi">14</span> <span class="mi">22</span><span class="p">:</span><span class="mi">05</span><span class="p">:</span><span class="mi">03</span>
<span class="o">>>>>>>>>></span> <span class="n">TestAdd</span>
<span class="o">>>></span> <span class="n">TestAdd</span><span class="p">.</span><span class="n">testAddError</span>
<span class="o">>>></span> <span class="n">TestAdd</span><span class="p">.</span><span class="n">testAddPositive</span>
<span class="o">>>></span> <span class="n">TestAdd</span><span class="p">.</span><span class="n">testAddZero</span>
<span class="o">>>></span> <span class="n">TestAdd</span><span class="p">.</span><span class="n">testAdder</span>
<span class="o">>>>>>>>>></span> <span class="n">TestDiv</span>
<span class="o">>>></span> <span class="n">TestDiv</span><span class="p">.</span><span class="n">testDivError</span>
<span class="o">>>></span> <span class="n">TestDiv</span><span class="p">.</span><span class="n">testDivPositive</span>
<span class="o">>>></span> <span class="n">TestDiv</span><span class="p">.</span><span class="n">testDivZero</span>
<span class="o">=========================================================</span>
<span class="n">Success</span><span class="p">:</span> <span class="mi">100</span><span class="o">%</span> <span class="o">-</span> <span class="mi">7</span> <span class="o">/</span> <span class="mi">7</span><span class="p">,</span> <span class="n">executed</span> <span class="k">in</span> <span class="mf">0.000</span> <span class="n">seconds</span>
</pre></div>
</div>
<p>When tests are defined in tables, you can optionally define two special
functions, <em>setUp()</em> and <em>tearDown()</em>, which will be executed
respectively before and after every test.</p>
<p>These function may be used to create specific resources for the
test being executed and cleanup the test environment.</p>
<p>For a practical example, imagine that we have a <em>log()</em> function
that writes strings to a log file on disk. The file is created
upon first usage of the function, and the filename is defined
by calling the function <em>initLog()</em>.</p>
<p>The tests for these functions would take advantage of the <em>setup/teardown</em>
functionality to prepare a log filename shared
by all tests, make sure that all tests start with a non existing
log file name, and erase the log filename after every test:</p>
<div class="highlight-lua"><div class="highlight"><pre><span class="n">TestLogger</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">function</span> <span class="nf">TestLogger</span><span class="p">:</span><span class="n">setUp</span><span class="p">()</span>
<span class="c1">-- define the fname to use for logging</span>
<span class="n">self</span><span class="p">.</span><span class="n">fname</span> <span class="o">=</span> <span class="s1">'</span><span class="s">mytmplog.log'</span>
<span class="c1">-- make sure the file does not already exists</span>
<span class="nb">os.remove</span><span class="p">(</span><span class="n">self</span><span class="p">.</span><span class="n">fname</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">function</span> <span class="nf">TestLogger</span><span class="p">:</span><span class="n">testLoggerCreatesFile</span><span class="p">()</span>
<span class="n">initLog</span><span class="p">(</span><span class="n">self</span><span class="p">.</span><span class="n">fname</span><span class="p">)</span>
<span class="n">log</span><span class="p">(</span><span class="s1">'</span><span class="s">toto'</span><span class="p">)</span>
<span class="c1">-- make sure that our log file was created</span>
<span class="n">f</span> <span class="o">=</span> <span class="nb">io.open</span><span class="p">(</span><span class="n">self</span><span class="p">.</span><span class="n">fname</span><span class="p">,</span> <span class="s1">'</span><span class="s">r'</span><span class="p">)</span>
<span class="n">assertNotNil</span><span class="p">(</span> <span class="n">f</span> <span class="p">)</span>
<span class="n">f</span><span class="p">:</span><span class="n">close</span><span class="p">()</span>
<span class="k">end</span>
<span class="k">function</span> <span class="nf">TestLogger</span><span class="p">:</span><span class="n">tearDown</span><span class="p">()</span>
<span class="c1">-- cleanup our log file after all tests</span>
<span class="nb">os.remove</span><span class="p">(</span><span class="n">self</span><span class="p">.</span><span class="n">fname</span><span class="p">)</span>
<span class="k">end</span>
</pre></div>
</div>
<dl class="docutils">
<dt><strong>Note:</strong></dt>
<dd><em>Errors generated during execution of setUp() or tearDown()
functions are considered test failures.</em></dd>
</dl>
</div>
<div class="section" id="using-the-command-line">
<h3>Using the command-line<a class="headerlink" href="#using-the-command-line" title="Permalink to this headline">¶</a></h3>
<p>You can control the LuaUnit execution from the command-line:</p>
<p><strong>Output format</strong></p>
<p>Choose the test output format with <tt class="docutils literal"><span class="pre">-o</span></tt> or <tt class="docutils literal"><span class="pre">--output</span></tt>. Available formats are:</p>
<ul class="simple">
<li>text: the default output format</li>
<li>nil: no output at all</li>
<li>TAP: TAP format</li>
<li>junit: output junit xml</li>
</ul>
<p>Example:</p>
<div class="highlight-lua"><div class="highlight"><pre><span class="n">lua</span> <span class="n">my_test_suite</span><span class="p">.</span><span class="n">lua</span> <span class="o">-</span><span class="n">o</span> <span class="n">TAP</span>
</pre></div>
</div>
<p><strong>List of tests to run</strong></p>
<p>You can list some test names on the command-line to run only those tests.
The name must be the exact match of either the test table, the test function or the test table
and the test method. The option may be repeated.</p>
<p>Example:</p>
<div class="highlight-lua"><div class="highlight"><pre><span class="c1">-- Run all TestAdd table tests and one test of TestDiv table.</span>
<span class="n">lua</span> <span class="n">my_test_suite</span><span class="p">.</span><span class="n">lua</span> <span class="n">TestAdd</span> <span class="n">TestDiv</span><span class="p">.</span><span class="n">testDivError</span>
</pre></div>
</div>
<p><strong>Filtering tests</strong></p>
<p>The most flexible approach for selecting tests to run is to use a pattern. With
<tt class="docutils literal"><span class="pre">--pattern</span></tt> or <tt class="docutils literal"><span class="pre">-p</span></tt>, you can provide a lua pattern and only the tests that contain
the pattern will actually be run.</p>
<p>Example:</p>
<div class="highlight-lua"><div class="highlight"><pre><span class="c1">-- Run all tests of zero testing and error testing</span>
<span class="c1">-- by using the magic character .</span>
<span class="n">lua</span> <span class="n">my_test_suite</span><span class="p">.</span><span class="n">lua</span> <span class="o">-</span><span class="n">p</span> <span class="n">Err</span><span class="p">.</span><span class="n">r</span> <span class="o">-</span><span class="n">p</span> <span class="n">Z</span><span class="p">.</span><span class="n">ro</span>
</pre></div>
</div>
<p>For our test suite, it gives the following output:</p>
<div class="highlight-lua"><div class="highlight"><pre><span class="n">Started</span> <span class="n">on</span> <span class="mi">08</span><span class="o">/</span><span class="mi">16</span><span class="o">/</span><span class="mi">14</span> <span class="mi">22</span><span class="p">:</span><span class="mi">38</span><span class="p">:</span><span class="mi">30</span>
<span class="o">>>>>>>>>></span> <span class="n">TestAdd</span>
<span class="o">>>></span> <span class="n">TestAdd</span><span class="p">.</span><span class="n">testAddError</span>
<span class="o">>>></span> <span class="n">TestAdd</span><span class="p">.</span><span class="n">testAddZero</span>
<span class="o">>>>>>>>>></span> <span class="n">TestDiv</span>
<span class="o">>>></span> <span class="n">TestDiv</span><span class="p">.</span><span class="n">testDivError</span>
<span class="o">>>></span> <span class="n">TestDiv</span><span class="p">.</span><span class="n">testDivZero</span>
<span class="o">=========================================================</span>
<span class="n">Success</span><span class="p">:</span> <span class="mi">100</span><span class="o">%</span> <span class="o">-</span> <span class="mi">4</span> <span class="o">/</span> <span class="mi">4</span><span class="p">,</span> <span class="n">executed</span> <span class="k">in</span> <span class="mf">0.000</span> <span class="n">seconds</span>
</pre></div>
</div>
<p>The pattern can be any lua pattern. Be sure to exclude all magic
characters with % (like -+?*) and protect your pattern from the shell
interpretation by putting it in quotes.</p>
</div>
<div class="section" id="conclusion">
<h3>Conclusion<a class="headerlink" href="#conclusion" title="Permalink to this headline">¶</a></h3>
<p>You now know enough of LuaUnit to start writing your test suite. Check
the reference documentation for a complete list of
assertions, command-line options and specific behavior.</p>
</div>
</div>
<div class="section" id="reference-documentation">
<span id="id1"></span><h2>Reference documentation<a class="headerlink" href="#reference-documentation" title="Permalink to this headline">¶</a></h2>
<div class="section" id="luaunit-run-function">
<span id="luaunit-run"></span><h3>LuaUnit.run() function<a class="headerlink" href="#luaunit-run-function" title="Permalink to this headline">¶</a></h3>
<p><strong>Return value</strong></p>
<p>Normally, you should run your test suite with the following line:</p>
<div class="highlight-lua"><div class="highlight"><pre><span class="nb">os.exit</span><span class="p">(</span><span class="n">LuaUnit</span><span class="p">.</span><span class="n">run</span><span class="p">())</span>
</pre></div>
</div>
<p>The <em>run()</em> function returns the number of failures of the test suite. This is
good for an exit code, 0 meaning success.</p>
<p><strong>Arguments</strong></p>
<p>If no arguments are supplied, it parses the command-line arguments of the script
and interpret them. If arguments are supplied to the function, they are parsed
instead of the command-line. It uses the same syntax.</p>
<p>Example:</p>
<div class="highlight-lua"><div class="highlight"><pre><span class="c1">-- execute tests matching the 'withXY' pattern</span>
<span class="nb">os.exit</span><span class="p">(</span><span class="n">LuaUnit</span><span class="p">.</span><span class="n">run</span><span class="p">(</span><span class="s1">'</span><span class="s">--pattern'</span><span class="p">,</span> <span class="s1">'</span><span class="s">withXY'</span><span class="p">))</span>
</pre></div>
</div>
<p><strong>Choice of tests</strong></p>
<p>If test names were supplied, only those
tests are executed. When test names are supplied, they don’t have
to start with <em>test</em>, they are run anyway.</p>
<p>If no test names were supplied, a general test collection process starts
under the following rules:</p>
<ul>
<li><p class="first">all variable starting with <em>test</em> or <em>Test</em> are scanned.</p>
</li>
<li><p class="first">if the variable is a function it is collected for testing</p>
</li>
<li><p class="first">if the variable is a table:</p>
<blockquote>
<div><ul class="simple">
<li>all keys starting with <em>test</em> or <em>Test</em> are collected (provided that they are functions)</li>
<li>keys with name <em>setUp</em> and <em>tearDown</em> are also collected</li>
</ul>
</div></blockquote>
</li>
</ul>
<p>If one or more pattern were supplied, the test are then filtered according the
pattern(s). Only the test that match the pattern(s) are actually executed.</p>
<p><strong>setup and teardown</strong></p>
<p>The function <em>setUp()</em> is executed before each test if it exists in the table.
The function <em>tearDown()</em> is executed after every test if it exists in the table.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><em>tearDown()</em> is always executed if it exists, even if there was a failure in the test or in the <em>setUp()</em> function.
Failures in <em>setUp()</em> or <em>tearDown()</em> are considered as a general test failures.</p>
</div>
</div>
<div class="section" id="luaunit-runsuite-function">
<h3>LuaUnit.runSuite() function<a class="headerlink" href="#luaunit-runsuite-function" title="Permalink to this headline">¶</a></h3>
<p>If you want to keep the flexibility of the command-line parsing, but want to force
some parameters, like the output format, you must use a slightly different syntax:</p>
<div class="highlight-lua"><div class="highlight"><pre><span class="n">lu</span> <span class="o">=</span> <span class="n">LuaUnit</span><span class="p">.</span><span class="n">new</span><span class="p">()</span>
<span class="n">lu</span><span class="p">:</span><span class="n">setOutputType</span><span class="p">(</span><span class="s2">"</span><span class="s">tap"</span><span class="p">)</span>
<span class="nb">os.exit</span><span class="p">(</span> <span class="n">lu</span><span class="p">:</span><span class="n">runSuite</span><span class="p">()</span> <span class="p">)</span>
</pre></div>
</div>
<p><em>runSuite()</em> behaves like <em>run()</em> except that it must be started
with a LuaUnit instance as first argument, and it will use the LuaUnit
instance settings.</p>
</div>
<div class="section" id="command-line-options">
<span id="command-line"></span><h3>Command-line options<a class="headerlink" href="#command-line-options" title="Permalink to this headline">¶</a></h3>
<p>Usage: lua <your_test_suite.lua> [options] [testname1 [testname2]</p>
<p><strong>Test names</strong></p>
<p>When no test names are supplied, all tests are collected.</p>
<p>The syntax for supplying test names can be either: name of the function, name of the table
or name of the table + ‘.’ + name of the function. Only the supplied tests will be executed.</p>
<p><strong>Selecting output format</strong></p>
<p>Choose the output format with the syntax <tt class="docutils literal"><span class="pre">-o</span> <span class="pre">FORMAT</span></tt> or <tt class="docutils literal"><span class="pre">--output</span> <span class="pre">FORMAT</span></tt>.</p>
<p>Formats available:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">text</span></tt>: the default output format of LuaUnit</li>
<li><tt class="docutils literal"><span class="pre">nil</span></tt>: no output at all</li>
<li><tt class="docutils literal"><span class="pre">tap</span></tt>: output compatible with the <a class="reference external" href="http://testanything.org/">Test Anything Protocol</a></li>
<li><tt class="docutils literal"><span class="pre">junit</span></tt>: output compatible with the <em>JUnit xml</em> format</li>
</ul>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">In the JUnit format, a destination filename must be supplied with <tt class="docutils literal"><span class="pre">--name</span></tt> or <tt class="docutils literal"><span class="pre">-n</span></tt></p>
</div>
<p><strong>Destination filename</strong></p>
<p>When using the JUnit format, the test suites writes an XML file with the test results. The
file name is mandatory and must be supplied with: <tt class="docutils literal"><span class="pre">--name</span> <span class="pre">FILENAME</span></tt> or <tt class="docutils literal"><span class="pre">-n</span> <span class="pre">FILENAME</span></tt></p>
<p><strong>Selecting tests with patterns</strong></p>
<p>You select a subset of tests by specifying one or more filter patterns,
with <tt class="docutils literal"><span class="pre">-p</span> <span class="pre">PATTERN</span></tt> or <tt class="docutils literal"><span class="pre">--pattern</span> <span class="pre">PATTERN</span></tt>.</p>
<p>The pattern is looked for on the full test name <em>TestTable.testMethod</em> . Only the tests that
actually match the pattern are selected. When specifying more than one pattern,
they are tried one by one until the name matches (OR combination).</p>
<p>Make sure you esape magic chars like <tt class="docutils literal"><span class="pre">+?-*</span></tt> with <tt class="docutils literal"><span class="pre">%</span></tt> .</p>
<p><strong>Other Options:</strong></p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">-h</span></tt>, <tt class="docutils literal"><span class="pre">--help</span></tt>: display the command-line help.</li>
<li><tt class="docutils literal"><span class="pre">--version</span></tt>: display the version information</li>
<li><tt class="docutils literal"><span class="pre">-v</span></tt>, <tt class="docutils literal"><span class="pre">--verbose</span></tt>: Increase the output verbosity. The exact effect depends on the output format. May be specified multiple times.</li>
<li><tt class="docutils literal"><span class="pre">-q</span></tt>, <tt class="docutils literal"><span class="pre">--quiet</span></tt>: Set verbosity to minimum. The exact effect depends on the output format.</li>
</ul>
</div>
<div class="section" id="assertions-functions">
<span id="assertions-label"></span><h3>Assertions functions<a class="headerlink" href="#assertions-functions" title="Permalink to this headline">¶</a></h3>
<p>You will now find the list of all assertion functions. For all functions, When an assertion fails, the failure
message tries to be as informative as possible, by displaying the expectation and value that caused the failure.</p>
<div class="section" id="equality-assertions">
<span id="assert-equality"></span><h4>Equality assertions<a class="headerlink" href="#equality-assertions" title="Permalink to this headline">¶</a></h4>
<p>All equality assertions functions take two arguments, in the order
<em>actual value</em> then <em>expected value</em>. Some people are more familiar
with the order <em>expected value</em> then <em>actual value</em>. It is possible to configure
LuaUnit to use the opposite order for all equality assertions, by setting up a global
variable:</p>
<div class="highlight-lua"><div class="highlight"><pre><span class="n">ORDER_ACTUAL_EXPECTED</span><span class="o">=</span><span class="kc">false</span>
</pre></div>
</div>
<p>The order only matters for the message that is displayed in case of failures. It does
not influence the test itself.</p>
<dl class="function">
<dt id="assertEquals">
<tt class="descname">assertEquals</tt><big>(</big><em>actual</em>, <em>expected</em><big>)</big><a class="headerlink" href="#assertEquals" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that two values are equal.</p>
<p>For tables, the comparison is a deep comparison :</p>
<ul class="simple">
<li>number of elements must be the same</li>
<li>tables must contain the same keys</li>
<li>each key must contain the same values. The values
are also compared recursively with deep comparison.</li>
</ul>
<p>LuaUnit provides other table-related assertions, see <a class="reference internal" href="#assert-table"><em>Table assertions</em></a></p>
</dd></dl>
<dl class="function">
<dt id="assertNotEquals">
<tt class="descname">assertNotEquals</tt><big>(</big><em>actual</em>, <em>expected</em><big>)</big><a class="headerlink" href="#assertNotEquals" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that two values are different. The assertion
fails if the two values are identical.</p>
<p>It also uses table deep comparison.</p>
</dd></dl>
<dl class="function">
<dt id="assertAlmostEquals">
<tt class="descname">assertAlmostEquals</tt><big>(</big><em>actual</em>, <em>expected</em>, <em>margin</em><big>)</big><a class="headerlink" href="#assertAlmostEquals" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that two floating point numbers are almost equal.</p>
<p>When comparing floating point numbers, strict equality does not work.
Computer arithmetic is so that an operation that mathematically
yields 1.00000000 might yield 0.999999999999 in lua . That’s why you
need an <em>almost equals</em> comparison, where you specify the error margin.</p>
</dd></dl>
<dl class="function">
<dt id="assertNotAlmostEquals">
<tt class="descname">assertNotAlmostEquals</tt><big>(</big><em>actual</em>, <em>expected</em>, <em>margin</em><big>)</big><a class="headerlink" href="#assertNotAlmostEquals" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that two floating point numbers are not almost equal.</p>
</dd></dl>
</div>
<div class="section" id="value-assertions">
<span id="assert-value"></span><h4>Value assertions<a class="headerlink" href="#value-assertions" title="Permalink to this headline">¶</a></h4>
<dl class="function">
<dt id="assertTrue">
<tt class="descname">assertTrue</tt><big>(</big><em>value</em><big>)</big><a class="headerlink" href="#assertTrue" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that a given value compares to true. Lua coercion rules are applied
so that values like <tt class="docutils literal"><span class="pre">0</span></tt>, <tt class="docutils literal"><span class="pre">""</span></tt>, <tt class="docutils literal"><span class="pre">1.17</span></tt> all compare to <em>true</em>.</p>
</dd></dl>
<dl class="function">
<dt id="assertFalse">
<tt class="descname">assertFalse</tt><big>(</big><em>value</em><big>)</big><a class="headerlink" href="#assertFalse" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that a given value compares to false. Lua coercion rules are applied
so that only <em>nil</em> and <em>false</em> all compare to <em>false</em>.</p>
</dd></dl>
<dl class="function">
<dt id="assertNil">
<tt class="descname">assertNil</tt><big>(</big><em>value</em><big>)</big><a class="headerlink" href="#assertNil" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that a given value is <em>nil</em> .</p>
</dd></dl>
<dl class="function">
<dt id="assertNotNil">
<tt class="descname">assertNotNil</tt><big>(</big><em>value</em><big>)</big><a class="headerlink" href="#assertNotNil" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that a given value is not <em>nil</em> . Lua coercion rules are applied
so that values like <tt class="docutils literal"><span class="pre">0</span></tt>, <tt class="docutils literal"><span class="pre">""</span></tt>, <tt class="docutils literal"><span class="pre">false</span></tt> all validate the assertion.</p>
</dd></dl>
<dl class="function">
<dt id="assertIs">
<tt class="descname">assertIs</tt><big>(</big><em>actual</em>, <em>expected</em><big>)</big><a class="headerlink" href="#assertIs" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that two variables are identical. For string, numbers, boolean and for nil,
this gives the same result as <a class="reference internal" href="#assertEquals" title="assertEquals"><tt class="xref py py-func docutils literal"><span class="pre">assertEquals()</span></tt></a> . For the other types, identity
means that the two variables refer to the same object.</p>
<p><strong>Example :</strong></p>
</dd></dl>
<div class="highlight-lua"><div class="highlight"><pre><span class="n">s1</span><span class="o">=</span><span class="s1">'</span><span class="s">toto'</span>
<span class="n">s2</span><span class="o">=</span><span class="s1">'</span><span class="s">to'</span><span class="o">..</span><span class="s1">'</span><span class="s">to'</span>
<span class="n">t1</span><span class="o">=</span><span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">}</span>
<span class="n">t2</span><span class="o">=</span><span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">}</span>
<span class="n">assertIs</span><span class="p">(</span><span class="n">s1</span><span class="p">,</span><span class="n">s1</span><span class="p">)</span> <span class="c1">-- ok</span>
<span class="n">assertIs</span><span class="p">(</span><span class="n">s1</span><span class="p">,</span><span class="n">s2</span><span class="p">)</span> <span class="c1">-- ok</span>
<span class="n">assertIs</span><span class="p">(</span><span class="n">t1</span><span class="p">,</span><span class="n">t1</span><span class="p">)</span> <span class="c1">-- ok</span>
<span class="n">assertIs</span><span class="p">(</span><span class="n">t1</span><span class="p">,</span><span class="n">t2</span><span class="p">)</span> <span class="c1">-- fail</span>
</pre></div>
</div>
<dl class="function">
<dt id="assertNotIs">
<tt class="descname">assertNotIs</tt><big>(</big><em>actual</em>, <em>expected</em><big>)</big><a class="headerlink" href="#assertNotIs" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that two variables are not identical, in the sense that they do not
refer to the same value. See <a class="reference internal" href="#assertIs" title="assertIs"><tt class="xref py py-func docutils literal"><span class="pre">assertIs()</span></tt></a> for more details.</p>
</dd></dl>
</div>
<div class="section" id="string-assertions">
<span id="assert-string"></span><h4>String assertions<a class="headerlink" href="#string-assertions" title="Permalink to this headline">¶</a></h4>
<p>Assertions related to string and patterns.</p>
<dl class="function">
<dt id="assertStrContains">
<tt class="descname">assertStrContains</tt><big>(</big><em>str</em>, <em>sub</em><span class="optional">[</span>, <em>useRe</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#assertStrContains" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that a string contains the given substring or pattern.</p>
<p>By default, substring is searched in the string. If <em>useRe</em>
is provided and is true, <em>sub</em> is treated as a pattern which
is searched inside the string <em>str</em> .</p>
</dd></dl>
<dl class="function">
<dt id="assertStrIContains">
<tt class="descname">assertStrIContains</tt><big>(</big><em>str</em>, <em>sub</em><big>)</big><a class="headerlink" href="#assertStrIContains" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that a string contains the given substring, irrespective of the case.</p>
<p>Not that unlike <tt class="xref py py-func docutils literal"><span class="pre">assertStrcontains()</span></tt>, you can not search for a pattern.</p>
</dd></dl>
<dl class="function">
<dt id="assertNotStrContains">
<tt class="descname">assertNotStrContains</tt><big>(</big><em>str</em>, <em>sub</em>, <em>useRe</em><big>)</big><a class="headerlink" href="#assertNotStrContains" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that a string does not contain a given substring or pattern.</p>
<p>By default, substring is searched in the string. If <em>useRe</em>
is provided and is true, <em>sub</em> is treated as a pattern which
is searched inside the string <em>str</em> .</p>
</dd></dl>
<dl class="function">
<dt id="assertNotStrIContains">
<tt class="descname">assertNotStrIContains</tt><big>(</big><em>str</em>, <em>sub</em><big>)</big><a class="headerlink" href="#assertNotStrIContains" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that a string does not contain the given substring, irrespective of the case.</p>
<p>Not that unlike <tt class="xref py py-func docutils literal"><span class="pre">assertNotStrcontains()</span></tt>, you can not search for a pattern.</p>
</dd></dl>
<dl class="function">
<dt id="assertStrMatches">
<tt class="descname">assertStrMatches</tt><big>(</big><em>str</em>, <em>pattern</em><span class="optional">[</span>, <em>start</em><span class="optional">[</span>, <em>final</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#assertStrMatches" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that a string matches the full pattern <em>pattern</em>.</p>
<p>If <em>start</em> and <em>final</em> are not provided or are <em>nil</em>, the pattern must match the full string, from start to end. The
functions allows to specify the expected start and end position of the pattern in the string.</p>
</dd></dl>
</div>
<div class="section" id="error-assertions">
<span id="assert-error"></span><h4>Error assertions<a class="headerlink" href="#error-assertions" title="Permalink to this headline">¶</a></h4>
<p>Error related assertions, to verify error generation and error messages.</p>
<dl class="function">
<dt id="assertError">
<tt class="descname">assertError</tt><big>(</big><em>func</em>, <em>...</em><big>)</big><a class="headerlink" href="#assertError" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that calling functions <em>func</em> with the arguments yields an error. If the
function does not yield an error, the assertion fails.</p>
<p>Note that the error message itself is not checked, which means that this function
does not distinguish between the legitimate error that you expect and another error
that might be triggered by mistake.</p>
<p>The next functions provide a better approach to error testing, by checking
explicitly the error message content.</p>
</dd></dl>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">When testing LuaUnit, switching from <em>assertError()</em> to <em>assertErrorMsgEquals()</em>
revealed quite a few bugs!</p>
</div>
<dl class="function">
<dt id="assertErrorMsgEquals">
<tt class="descname">assertErrorMsgEquals</tt><big>(</big><em>expectedMsg</em>, <em>func</em>, <em>...</em><big>)</big><a class="headerlink" href="#assertErrorMsgEquals" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that calling function <em>func</em> will generate exactly the given error message. If the
function does not yield an error, or if the error message is not identical, the assertion fails.</p>
<p>Be careful when using this function that error messages usually contain the file name and
line number information of where the error was generated. This is usually inconvenient. To
ignore the filename and line number information, you can either use a pattern with <a class="reference internal" href="#assertErrorMsgMatches" title="assertErrorMsgMatches"><tt class="xref py py-func docutils literal"><span class="pre">assertErrorMsgMatches()</span></tt></a>
or simply check for the message containt with <a class="reference internal" href="#assertErrorMsgContains" title="assertErrorMsgContains"><tt class="xref py py-func docutils literal"><span class="pre">assertErrorMsgContains()</span></tt></a> .</p>
</dd></dl>
<dl class="function">
<dt id="assertErrorMsgContains">
<tt class="descname">assertErrorMsgContains</tt><big>(</big><em>partialMsg</em>, <em>func</em>, <em>...</em><big>)</big><a class="headerlink" href="#assertErrorMsgContains" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that calling function <em>func</em> will generate an error message containing <em>partialMsg</em> . If the
function does not yield an error, or if the expected message is not contained in the error message, the
assertion fails.</p>
</dd></dl>
<dl class="function">
<dt id="assertErrorMsgMatches">
<tt class="descname">assertErrorMsgMatches</tt><big>(</big><em>expectedPattern</em>, <em>func</em>, <em>...</em><big>)</big><a class="headerlink" href="#assertErrorMsgMatches" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that calling function <em>func</em> will generate an error message matching <em>expectedPattern</em> . If the
function does not yield an error, or if the error message does not match the provided patternm the
assertion fails.</p>
<p>Note that matching is done from the start to the end of the error message. Be sure to escape magic all magic
characters with <tt class="docutils literal"><span class="pre">%</span></tt> (like <tt class="docutils literal"><span class="pre">-+.?*</span></tt>) .</p>
</dd></dl>
</div>
<div class="section" id="type-assertions">
<span id="assert-type"></span><h4>Type assertions<a class="headerlink" href="#type-assertions" title="Permalink to this headline">¶</a></h4>
<blockquote>
<div>The following functions all perform type checking on their argument. If the
received value is not of the right type, the failure message will contain
the expected type, the received type and the received value to help you
identify better the problem.</div></blockquote>
<dl class="function">
<dt id="assertIsNumber">
<tt class="descname">assertIsNumber</tt><big>(</big><em>value</em><big>)</big><a class="headerlink" href="#assertIsNumber" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that the argument is a number (integer or float)</p>
</dd></dl>
<dl class="function">
<dt id="assertIsString">
<tt class="descname">assertIsString</tt><big>(</big><em>value</em><big>)</big><a class="headerlink" href="#assertIsString" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that the argument is a string.</p>
</dd></dl>
<dl class="function">
<dt id="assertIsTable">
<tt class="descname">assertIsTable</tt><big>(</big><em>value</em><big>)</big><a class="headerlink" href="#assertIsTable" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that the argument is a table.</p>
</dd></dl>
<dl class="function">
<dt id="assertIsBoolean">
<tt class="descname">assertIsBoolean</tt><big>(</big><em>value</em><big>)</big><a class="headerlink" href="#assertIsBoolean" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that the argument is a boolean.</p>
</dd></dl>
<dl class="function">
<dt id="assertIsNil">
<tt class="descname">assertIsNil</tt><big>(</big><em>value</em><big>)</big><a class="headerlink" href="#assertIsNil" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that the argument is a nil.</p>
</dd></dl>
<dl class="function">
<dt id="assertIsFunction">
<tt class="descname">assertIsFunction</tt><big>(</big><em>value</em><big>)</big><a class="headerlink" href="#assertIsFunction" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that the argument is a function.</p>
</dd></dl>
<dl class="function">
<dt id="assertIsUserdata">
<tt class="descname">assertIsUserdata</tt><big>(</big><em>value</em><big>)</big><a class="headerlink" href="#assertIsUserdata" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that the argument is a userdata.</p>
</dd></dl>
<dl class="function">
<dt id="assertIsCoroutine">
<tt class="descname">assertIsCoroutine</tt><big>(</big><em>value</em><big>)</big><a class="headerlink" href="#assertIsCoroutine" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that the argument is a coroutine (an object with type <em>thread</em> ).</p>
</dd></dl>
<dl class="function">
<dt id="assertIsThread">
<tt class="descname">assertIsThread</tt><big>(</big><em>value</em><big>)</big><a class="headerlink" href="#assertIsThread" title="Permalink to this definition">¶</a></dt>
<dd><p>An alias for <a class="reference internal" href="#assertIsCoroutine" title="assertIsCoroutine"><tt class="xref py py-func docutils literal"><span class="pre">assertIsCoroutine()</span></tt></a>.</p>
</dd></dl>
</div>
<div class="section" id="table-assertions">
<span id="assert-table"></span><h4>Table assertions<a class="headerlink" href="#table-assertions" title="Permalink to this headline">¶</a></h4>
<dl class="function">
<dt id="assertItemsEquals">
<tt class="descname">assertItemsEquals</tt><big>(</big><em>actual</em>, <em>expected</em><big>)</big><a class="headerlink" href="#assertItemsEquals" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that two tables contain the same items, irrespective of their keys.</p>
<p>This function is practical for example if you want to compare two lists but
where items are not in the same order:</p>
</dd></dl>
<div class="highlight-lua"><div class="highlight"><pre><span class="n">assertItemsEquals</span><span class="p">(</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">},</span> <span class="p">{</span><span class="mi">3</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">}</span> <span class="p">)</span> <span class="c1">-- assertion succeeds</span>
</pre></div>
</div>
<blockquote>
<div>The comparison is not recursive on the items: if any of the items are tables,
they are compared using table equality (like as in <a class="reference internal" href="#assertEquals" title="assertEquals"><tt class="xref py py-func docutils literal"><span class="pre">assertEquals()</span></tt></a> ), where
the key matters.</div></blockquote>
<div class="highlight-lua"><div class="highlight"><pre><span class="n">assertItemsEquals</span><span class="p">(</span> <span class="p">{</span><span class="mi">1</span><span class="p">,{</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">},</span><span class="mi">4</span><span class="p">},</span> <span class="p">{</span><span class="mi">4</span><span class="p">,{</span><span class="mi">3</span><span class="p">,</span><span class="mi">2</span><span class="p">,},</span><span class="mi">1</span><span class="p">}</span> <span class="p">)</span> <span class="c1">-- assertion fails because {2,3} ~= {3,2}</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="section" id="annex-index-and-search-page">
<h1>Annex: index and search page<a class="headerlink" href="#annex-index-and-search-page" title="Permalink to this headline">¶</a></h1>
<ul class="simple">
<li><a class="reference internal" href="genindex.html"><em>Index</em></a></li>
<li><a class="reference internal" href="search.html"><em>Search Page</em></a></li>
</ul>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="#">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Welcome to LuaUnit’s documentation!</a><ul>
<li><a class="reference internal" href="#introduction">Introduction</a><ul>
<li><a class="reference internal" href="#platform-support">Platform support</a></li>
<li><a class="reference internal" href="#development">Development</a></li>
</ul>
</li>
<li><a class="reference internal" href="#getting-started">Getting started</a><ul>
<li><a class="reference internal" href="#setting-up-your-test-script">Setting up your test script</a></li>
<li><a class="reference internal" href="#writing-tests">Writing tests</a></li>
<li><a class="reference internal" href="#grouping-tests-setup-teardown-functionality">Grouping tests, setup/teardown functionality</a></li>
<li><a class="reference internal" href="#using-the-command-line">Using the command-line</a></li>
<li><a class="reference internal" href="#conclusion">Conclusion</a></li>
</ul>
</li>
<li><a class="reference internal" href="#reference-documentation">Reference documentation</a><ul>
<li><a class="reference internal" href="#luaunit-run-function">LuaUnit.run() function</a></li>
<li><a class="reference internal" href="#luaunit-runsuite-function">LuaUnit.runSuite() function</a></li>
<li><a class="reference internal" href="#command-line-options">Command-line options</a></li>
<li><a class="reference internal" href="#assertions-functions">Assertions functions</a><ul>
<li><a class="reference internal" href="#equality-assertions">Equality assertions</a></li>
<li><a class="reference internal" href="#value-assertions">Value assertions</a></li>
<li><a class="reference internal" href="#string-assertions">String assertions</a></li>
<li><a class="reference internal" href="#error-assertions">Error assertions</a></li>
<li><a class="reference internal" href="#type-assertions">Type assertions</a></li>
<li><a class="reference internal" href="#table-assertions">Table assertions</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#annex-index-and-search-page">Annex: index and search page</a></li>
</ul>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/index.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li><a href="#">LuaUnit 1.6 documentation</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2014, Philippe Fremy.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.2.
</div>
</body>
</html>
|