A first test with quantum test
Task: check that a page lists what is in the database, that its action stores a row and says so, and that it refuses bad input — without writing Python.
A small app: one table, a page that lists it and an action that adds to it.
# Recipe: a first test with `quantum test`.
paths:
components: ./components
migrations: ./migrations
datasources:
db:
driver: sqlite
database: ./data/notes.dbCREATE TABLE notes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL
);
INSERT INTO notes (title) VALUES ('Read the guide');<q:component name="Notes">
<q:action name="add" method="POST">
<q:param name="title" required="true" minlength="3" />
<q:query name="added" datasource="db">
INSERT INTO notes (title) VALUES (:title)
<q:param name="title" value="{title}" type="string" />
</q:query>
<q:redirect url="/" flash="Added: {title}" />
</q:action>
<q:query name="notes" datasource="db">SELECT title FROM notes ORDER BY id</q:query>
<h1>Notes ({notes_result.recordCount})</h1>
<q:if condition="flash"><p class="flash">{flash}</p></q:if>
<form method="POST" action="/?action=add">
<input name="title" />
<button>Add</button>
</form>
<ul>
<q:loop query="notes"><li>{notes.title}</li></q:loop>
</ul>
</q:component>The tests sit next to it, in tests/. Each q:test starts from a fresh database built from migrations/, so they do not depend on each other:
<q:test name="the page lists the notes" page="/">
<test:visit />
<test:expect status="200" text="Notes (1)" />
<test:expect text="Read the guide" />
</q:test>
<q:test name="adding a note stores it and says so" page="/">
<test:submit action="add" title="Buy bread" />
<test:expect redirect="/" flash="Added: Buy bread" />
<test:expect table="notes" count="1" where="title = 'Buy bread'" />
<test:expect text="Notes (2)" />
</q:test>
<q:test name="a title that is too short is refused on its field" page="/">
<test:submit action="add" title="x" />
<test:expect error="title" />
<test:expect table="notes" count="1" />
</q:test>Run them from the app's folder:
quantum testtests/notes.test.q
PASS the page lists the notes
PASS adding a note stores it and says so
PASS a title that is too short is refused on its field
3 passed, 0 failedtest:visit opens the page; test:submit posts an action with the other attributes as its fields; test:expect checks what happened — the status, the redirect and the flash, a text on the page, rows in a table, or the field an input was refused on. The whole vocabulary is in the Testing an App guide.
Tested: this page imports the files of examples/cookbook/testing/first-test/, and the result above is the report of running them.