Pular para o conteúdo

Um primeiro teste com quantum test ​

Tradução automática

Esta página foi traduzida automaticamente do inglês e ainda não foi revisada por um falante nativo; correções são bem-vindas no GitHub. Se algo não bater, vale o original em inglês. O código e os resultados são os mesmos do original, importados dos arquivos testados.

Tarefa: conferir que uma página lista o que está no banco, que a sua ação grava uma linha e avisa, e que recusa uma entrada ruim — sem escrever Python.

Uma aplicação pequena: uma tabela, uma página que a lista e uma ação que acrescenta a ela.

yaml
# Recipe: a first test with `quantum test`.
paths:
  components: ./components
  migrations: ./migrations

datasources:
  db:
    driver: sqlite
    database: ./data/notes.db
sql
CREATE TABLE notes (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title TEXT NOT NULL
);
INSERT INTO notes (title) VALUES ('Read the guide');
xml
<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>

Os testes ficam ao lado, em tests/. Cada q:test começa de um banco novo construído a partir de migrations/, então um não depende do outro:

xml
<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>

Rode a partir da pasta da aplicação:

bash
quantum test
text
tests/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 failed

test:visit abre a página; test:submit envia uma ação com os outros atributos como campos; test:expect confere o que aconteceu — o status, o redirecionamento e a mensagem flash, um texto na página, linhas numa tabela, ou o campo em que uma entrada foi recusada. O vocabulário completo está no guia Testing an App (em inglês).

Testado: esta página importa os arquivos de examples/cookbook/testing/first-test/, e o resultado acima é o relatório de rodá-los.

Licença MIT · Feito com VitePress