Hot answers tagged

6

When you ran emacs ~/robe/core-tests.el, that opened up the file in your Emacs, right? Then the tests themselves have to be loaded into Emacs. The ert tests are written in ELisp, so you need to evaluate that file. That can be done with M-x eval-buffer, or if the file is not open, you can run M-x load-file. Then M-x ert will run those tests. Simply opening ...


6

Idle timers won't be touched until the call stack has been cleared, so sitting won't actually help, it is only once all execution has stopped with the timer be run, so in your case, the idle timer won't run until 3 seconds of idle time after your whole test has run. To combat this, you can call ert-run-idle-timers from ert-x.el in your test.


6

I would place the tests in a test directory and name it thepackage-test.el. Alternatively, thepackage-test-onething.el and thepackage-test-anotherthing.el. Using a test directory is especially useful if the tests require auxiliary files. Another advantage is that it simplifies the MELPA recipe, as subdirectories aren't included by default. For an example ...


6

M-x info -> g (ert). ERT is a tool for automated testing in Emacs Lisp. Its main features are facilities for defining tests, running them and reporting the results, and for debugging test failures interactively. ERT is similar to tools for other environments such as JUnit, but has unique features that take advantage of the dynamic and ...


4

It's not specifically for "pending", but marking the test as expected to fail seems like it would be what you want: http://www.gnu.org/software/emacs/manual/html_node/ert/Expected-Failures.html add `:expected-result :failed' to the test definition: (ert-deftest future-bug () "Test `time-forward' with negative arguments. Since this functionality isn'...


4

Separate file, of course. The end users need not load it. In fact, if you distribute through MELPA, the tests won't be bundled. The convention is to name the file foo-test.el.


3

Some window managers, such as MS Windows, automatically give a new frame the focus after they create it. If you need to focus another frame at that point then you will likely need to use function select-frame-set-input-focus.


3

A wild guess: put the file name in a defvar or a defconst, and use it in the function: (defconst my-test-location (file-name-directory (or load-file-name buffer-file-name))) (ert-deftest closing-braces () (find-file (concat my-test-location "ClosingBrace.file"))) What you want is the value of load-file-name or buffer-file-name at compile/load time, but ...


3

My take on this is that execute-kbd-macro acts on the buffer of the selected window (it is effectively typed keystrokes from the user, after all), and you must therefore ensure that this is set to your temporary buffer. e.g.: (with-temp-buffer (insert "select") (save-window-excursion (set-window-buffer nil (current-buffer)) (execute-kbd-macro (...


3

You can see how I solved a similar problem with lispy-test.el. Here's an example test: (ert-deftest lispy-braces () (should (string= (lispy-with "\"a regex \\\\|\"" "{") "\"a regex \\\\{|\\\\}\"")) (should (string= (lispy-with "\"a string |" "{") "\"a string {|}"))) lispy-with is a macro that creates a temporary ...


3

Let's use the example from the ERT manual: (ert-deftest ert-test-record-backtrace () (let ((test (make-ert-test :body (lambda () (ert-pass "foo"))))) (let ((result (ert-run-test test))) (should (ert-test-failed-p result))))) The test should pass obviously. A list of symbols naming an ert-test object will be returned with (apropos-internal "" #'...


3

You can try ert-run-tests-batch, e.g. (ert-run-tests-batch 'ert-test-record-backtrace) but the results are not returned as a value, but rather via messages in *Messages* (or on stdout). Otherwise, you can use ert-run-tests which is a bit more complex to use because you need to provide a listener function: (ert-run-tests 'ert-test-record-backtrace ...


2

I've used the following construct: (defvar objc-font-lock-test-setup-directory (if load-file-name (file-name-directory load-file-name) default-directory)) It works both when the file is loaded or when doing something like an eval-buffer.


2

Placing a call to ert-skip at the beginning of your test would be the solution I believe. If you'd like, you can pass the string "pending" to the function to better show why it was skipped. (ert-deftest my-pending-test () (ert-skip "pending") (should (dostuff)))


2

find-file calls switch-to-buffer which "Display buffer BUFFER-OR-NAME in the selected window." Once control returns to the command loop, it makes the buffer in the selected window the current. However, in your case, this never happens since you're still running the ert code. You can get around this by doing something like (set-buffer (window-buffer (selected-...


2

When a file is loaded, load-file-name is set. When a form is evaluated interactively from a file, buffer-file-name is set. So you can use (or load-file-name buffer-file-name) as the value of a global variable. You can then use file-name-directory and such to get parent directories. If you use the f.el file name library, you can use something like (f-parent (...


2

First, the idea is great, you should write this kind of test cases for your packages. One way to do this is to insert a suitable content into a temporary buffer, add suitable keys to the input queue, break into recursive edit, and let nature have its course. This way, things like post-command hooks are executed. For example: (defun test-upcase-...


2

I have used something like this in vc-tests.el: (ert-deftest vc-test-git00-create-repo () "Check `vc-create-repo' for the git backend." ...) (ert-deftest vc-test-git01-register () "Check `vc-register' and `vc-registered' for the git backend." (skip-unless (ert-test-passed-p (ert-test-most-recent-result (ert-get-test 'vc-test-git00-...


2

If you indent your code, you should end up with the following: (ert-deftest pp-test-process-function () (setq dc-current-buffer "/") (cl-letf (((symbol-function 'dc-process) (lambda (&rest params) "test")) (should (equal (dc-test-stub-process "a" "d" "c") "test2"))))) Which should make clear that you are actually binding a ...


2

(void-function loop) loop is not available, which comes from cl.el. In your code/tests, you should load this library before using loop. When you launch Emacs interactively with your own init.el, it is usually already loaded since it is a common library used by many other packages.


2

It turns out that this is intentional and the fault of ert-batch-backtrace-right-margin which is set to 70 by default. Its docstring instructs you: Use nil for no limit (caution: backtrace lines can be very long).


1

As far as I could tell, it is not possible to cause the selectors t (universe) or :new to run tests in the order of definition. The (member TEST1 TEST2 ...) selector however executes tests in the order specified. For my tests I am therefore using one of two workarounds (both written ad-hoc and not tested for balanced parentheses): 1. Wrapper, that records ...


1

One way is by string comparisons, e.g. (require 'ert) (ert-deftest test-1 () (should (string= (with-temp-buffer (org-mode) (insert " * test heading ") (goto-char (point-min)) (promote-next-heading) (buffer-string)) " ** test heading "))) You could also have content in files and do it like this: (ert-deftest ...


1

This is due to Bug#6581 - equal-including-properties uses eq to compare property values, hence your :candidate "type" property string values may compare non-eq. If you byte compile the test code, you might find it works because the compiler coalesces string literals (e.g., Bug#31688). You could consider using a symbol instead of a string, if that makes ...


1

I asked this question on gnu.emacs.help and learned how to make this work: (let ((debug-on-error nil)) (should-error (cl-assert nil))) It seems cl-assert doesn't raise an error if debug-on-error is true, it just goes straight to the debugger.


1

Maybe something like this: (defun func-one () (message "running: func-one") nil) (defun func-two () (message "running: func-two") (and (func-one) t)) (defun func-thr () (message "running: func three") (or (func-one) t)) (require 'ert) (ert-delete-all-tests) (setq func-tests nil) (ert-deftest test-one () "Test basic functionality" (when (...


1

I've been using ecukes to write these sorts of tests and it's brilliant. Test cases look like this: # Check *args works ok Scenario: Space *args on its own When I type "f(*args)" Then I should see "f(*args)" Scenario: Space *args with other args When I type "f(a,*args)" Then I should see "f(a, *args)" # And **kwargs Scenario: ...


Only top voted, non community-wiki answers of a minimum length are eligible