Beginner’s Guide to Creating Discourse Plugins Part 6: Acceptance Tests

testing
plugins

(Robin Ward) #1

Previous tutorials in this series:

Part 1: Creating a basic plugin
Part 2: Plugin outlets
Part 3: Custom Settings
Part 4: git setup
Part 5: Admin interfaces


Did you know that Discourse has two large test suites for its code base? On the server side, our Ruby code has a test suite that uses rspec. For the browser application, we have a qunit suite that has ember-testing included.

Assuming you have a development environment set up, if you visit the http://localhost:3000/qunit URL you will start running the Javascript test suite in your browser. One fun aspect is that you can see it testing the application in a miniature window in the bottom right corner:

Adding an Acceptance Test in your Plugin

First, make sure you have the latest version of Discourse checked out. Being able to run Acceptance tests from plugins is a relatively new feature, and if you don’t check out the latest version your tests won’t show up.

For this article I am going to write an acceptance test for the purple-tentacle plugin that we authored in part 5 of this series.

Adding an acceptance test is as easy as adding one file to your plugin. Create the following:

test/javascripts/acceptance/purple-tentacle-test.js.es6

import { acceptance } from "helpers/qunit-helpers";
acceptance("Purple Tentacle", { loggedIn: true });

test("Purple tentacle button works", assert => {
  visit("/admin/plugins/purple-tentacle");

  andThen(() => {
    assert.ok(exists('#show-tentacle'), "it shows the purple tentacle button");
    assert.ok(!exists('.tentacle'), "the tentacle is not shown yet");
  });

  click('#show-tentacle');

  andThen(() => {
    assert.ok(exists('.tentacle'), "the tentacle wants to rule the world!");
  });
});

I tried to write the test in a way that is clear, but it might be a little confusing if you’ve never written an acceptance test before. I highly recommend that you read the ember docs on acceptance testing as they have a lot of great information.

The first line of importance is visit("/admin/plugins/purple-tentacle");. This tells our test to navigate to that URL in our application. That URL was the one that displays the tentacle.

The next bit andThen() is a piece of code that will execute after the page has fully loaded. This might be a little confusing if you’ve not done much asynchronous programming before. Browser Applications like Discourse are built using javascript promises. Often, performing an action means that the Javascript application will have to contact the server and wait for a reply.

Visiting a URL almost always involves fetching data from a server, so in Ember all requests are considered asynchronous. That means that we need to write some code that will be called when the routing is finished and ember has loaded the URL. That’s where andThen() comes in.

It almost reads like english doesn’t it? “Visit a URL and THEN perform some tests”.

In this case, I am checking that when the page is initially visited, the #show-tentacle button exists, and that the purple tentacle is hidden. (P.S. the previous version of the purple-tentacle plugin didn’t have the #show-tentacle element id in the handlebars template. Check out the latest version to follow along!)

Once those tests pass it’s time to test the interaction. The next command is click('#show-tentacle'); which tells our testing framework that we want to click the button and show the tentacle.

Finally, we have another andThen function to wait until whatever processing happens when the button is clicked, and then makes sure that the purple tentacle is present.

Not too bad is it? You can try the test yourself by visiting http://localhost:3000/qunit?module=Acceptance%3A%20Purple%20Tentacle on your development machine. You should very quickly see the purtple tentacle appear and all tests will pass.

If you want to run the plugin qunit tests on the command line using PhantomJS, you can run

rake plugin:qunit['purple-tentacle']

(where purple-tentacle is the folder name of your plugin)

Where to go from here

I hate to sound like a broken record but the Ember documentation on testing is excellent. You might also want to see how Discourse tests various functionality by browsing the tests in our javascript tests directory. We have quite a few examples in there you can learn from.

Happy testing!


Plugin Outlet Locations
Suggestions for better plugin maintenance
Run Ruby code on page change
Running a single test
Setting up plugin continuous integration tests on Travis CI
Allow reply-to individual instead of topic/forum (mailing list feature)
Beginner's Guide to Creating Discourse Plugins - Part 1
Compatibility checks and stable versioning for plugins
Plugin acceptance tests showcase
Beginner's Guide to Creating Discourse Plugins Part 5: Admin Interfaces
Write Ember Acceptance and Component Tests for Discourse
How do I run only discourse/plugins/poll/spec?
Rails Girls 2015 SoC Banter
(Mittineague) #2

OMG, have I put off writing tests for this long?!

A few things
I noticed you put an “id” into the template file that wasn’t there in the previous tutorial
I added 3 more tests to deal with the “Hide Tentacle” button I added
I needed to be logged in it is an Admin page
I needed to use port 4000 for my VirtualBox Vagrant set-up

Happily, quick success !


(Joe Buhlig) #3

I’ve noticed a push to implement testing for plugins lately and I completely agree that this is important. I’ve been terrible at writing these in the past and want to get caught up.

I was considering the setup of Travis CI on a plugin repo as I noticed the ability to use Docker with Travis. But having never touched Travis before, is it possible to do this with a plugin? It would be nice to submit PRs to my own repo and have the tests run automatically.


Setting up plugin continuous integration tests on Travis CI
(Jen) #5

how can I run single ember tests on the browser?

http://localhost:3000/qunit?module=Acceptance%3A%20Purple%20Tentacle

I mean, if I have another acceptance test called “another test”, how would I run it? Thank you!


(Robin Ward) #6

If you click “rerun” beside a test, you’ll get a URL that is just for that particular test that you can refresh over and over.

You can also run by module by clicking in the “All Module” box and checking off just the modules you want to run.


Two Factor local login proposal
(David Taylor) #7

This tutorial needs updating to replace ok(...) with assert.ok(...).

Have put the necessary changes in a PR - I’m happy to edit the OP if it’s made into a wiki :slight_smile:


(Jeff Atwood) #8

OK it is wikified! PM me if anything else needs this treatment any time.


(Angus McLeod) #9

A note after writing some basic acceptance tests for the Elections Plugin:

  • If you use the default loggedIn option, you’re logged in as eviltrout with the properties in this session fixture

  • You can login as a custom user (necessary if you’ve added custom user properties) by defining your own session fixture and setting it as the current user using the beforeEach hook. See here for an example.

@eviltrout is there a better way of using a custom user for acceptance tests?


(Robin Ward) #10

What we should probably do is change the acceptance method in discourse to take an argument for the user to log in as. Then your test could say { loggedIn: true, logInAs: angus }

If you’ve got time for that PR we’d accept it!


#11

q: how to run the ruby tests tests for a plugin?

i am trying to run the tests for discourse-github-linkback, but i have no idea how.

After some stackoverflow i tried this (and i got some errors):

~/discourse$ bundle exec rspec ./plugins/discourse-github-linkback/spec/lib/github_linkback_spec.rb 

An error occurred while loading ./plugins/discourse-github-linkback/spec/lib/github_linkback_spec.rb.
Failure/Error: CoreExt::ActiveSupport.without_bootsnap_cache { super }

NameError:
  uninitialized constant GithubLinkback
# ./plugins/discourse-github-linkback/spec/lib/github_linkback_spec.rb:3:in `<top (required)>'
# ------------------
# --- Caused by: ---
# NameError:
#   uninitialized constant GithubLinkback
#   ./plugins/discourse-github-linkback/spec/lib/github_linkback_spec.rb:3:in `<top (required)>'
No examples found.

Randomized with seed 60987


Finished in 0.02025 seconds (files took 3.36 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

Randomized with seed 60987

Note that i see the plugin in the local discourse dev admin page, so i hope it is correctly installed (into ~discourse/plugins/


(Sam Saffron) #12

Simplest way, run bin/rake autospec, and save the file


#13

From what i see, doing cd ~/discourse and then running

runs all the tests for discourse.

On my ubuntu 18.04 VM with i5 6500, this has been running for half an hour already.
It feels wrong to run the whole tests suite when i only want to run the tests in that one plugin.
(this is especially so when i probably will modify my tests again, at least a few times)

Did i do something wrong?


(Sam Saffron) #14

Let me guess, you are symlinking your plugin vs checking out directly to the path under plugins


#15

nope,

tbp@tbp-linux-dev:~$ cd ~/discourse/plugins/
git clone -b allow-all-user-repositories-wildcard https://github.com/TheBestPessimist/discourse-github-linkback
tbp@tbp-linux-dev:~/discourse/plugins$ ll
total 40
drwxr-xr-x 10 tbp tbp 4096 mai 13 21:48 ./
drwxr-xr-x 20 tbp tbp 4096 mai 13 21:18 ../
drwxr-xr-x  6 tbp tbp 4096 mai 13 20:29 discourse-details/
drwxr-xr-x  6 tbp tbp 4096 mai 13 21:48 discourse-github-linkback/
drwxr-xr-x  6 tbp tbp 4096 mai 13 20:29 discourse-local-dates/
drwxr-xr-x  9 tbp tbp 4096 mai 13 20:29 discourse-narrative-bot/
drwxr-xr-x  7 tbp tbp 4096 mai 13 20:29 discourse-nginx-performance-report/
drwxr-xr-x  5 tbp tbp 4096 mai 13 20:29 discourse-presence/
drwxr-xr-x  3 tbp tbp 4096 mai 13 20:29 lazyYT/
drwxr-xr-x  9 tbp tbp 4096 mai 13 20:29 poll/
tbp@tbp-linux-dev:~/discourse/plugins$ 

(Sam Saffron) #16

Can you confirm you tried saving the spec file once it was running, cause it is meant to interrupt stuff and start running the plugin spec?


#17

since the initial bin/rake autospec is still running, there was/is no “save spec file” message yet.
also, by running bin/rake autospec --help, i saw no --save option.

I didn’t understand what save the file meant, so my full command was bin/rake autospec > spec.rb <- probably this is wrong


(Sam Saffron) #18

I meant in Vim or whatever you use, save the spec file, there is a watcher on it that will invoke the spec runner once it is saved.

Also see: How do I run only discourse/plugins/poll/spec?


#19

Finally i did it :^).

With the help of that link, i ran the tests by using this command:
bundle exec rake plugin:spec["discourse-github-linkback"]
and, ofc, i was in ~/discourse when doing that.

Thanks for all your help @sam!

(1st PR for discourse-plugin babyyyyy :tada:)