Save that to 'add7.m'. Now you can say 'doctest add7' and it will run
- 'add7(3)' and make sure that it gets back 'ans = 10'.
+ 'add7(3)' and make sure that it gets back 'ans = 10'. It prints out
+ This is in the Test Anything Protocol format, which I guess is mostly
+ used by Perl people, but it's good enough for now. See testanything.org.
If the output of some function will change each time you call it, for
instance if it includes a random number or a stack trace, you can put ***
(three asterisks) where the changing element should be. This acts as a
- wildcard, and will match anything.
+ wildcard, and will match anything. See the example below.
+ Running 'doctest doctest' will execute these examples and test the
+ Note the two blank lines between the end of the output and the beginning
+ of this paragraph. That's important so that we can tell that this
+ paragraph is text and not part of the example!
+ If there's no output, that's fine, just put the next line right after the
+ one with no output. If the line does produce output (for instance, an
+ error), this will be recorded as a test failure.
+ doctest can deal with errors, a little bit. For instance, this case is
+ >> not_a_real_function(42)
+ ??? Undefined function or method 'not_a_real_function' for input
+ arguments of type 'double'.
+ But if the line of code will emit other output BEFORE the error message,
+ the current version can't deal with that. For more info see Issue #4 on
+ the bitbucket site (below). Warnings are different from errors, and they
+ If you have something that has changing output, for instance line numbers
+ in a stack trace, or something with random numbers, you can use a
+ wildcard to match that part.
because I haven't found a good way to mark these subsequent lines as
part-of-the-source-code rather than part-of-the-result.
+ When you're working on writing/debugging a Matlab class, you might need
+ to run 'clear classes' to get correct results from doctests (this is a
+ general problem with developing classes in Matlab).
+ It doesn't say what line number/file the doctest error is in. This is
+ because it uses Matlab's plain ol' HELP function to extract the
+ documentation. It wouldn't be too hard to write our own comment parser,
+ but this hasn't happened yet. (See Issue #2 on the bitbucket site,
+Result of running 'doctest doctest':
+ok 4 - "not_a_real_function(42)"