Ir al contenido

Una primera prueba con quantum test ​

Traducción automática

Esta página se tradujo automáticamente del inglés y todavía no la revisó un hablante nativo; las correcciones son bienvenidas en GitHub. Si algo no coincide, vale el original en inglés.

Tarea: verificar que una página lista lo que hay en la base de datos, que su acción guarda una fila y lo dice, y que rechaza una entrada mala — sin escribir Python.

Una aplicación pequeña: una tabla, una página que la lista y una acción que le agrega filas.

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>

Las pruebas están al lado, en tests/. Cada q:test empieza con una base de datos nueva construida a partir de migrations/, así que no dependen unas de otras:

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>

Ejecútalas desde la carpeta de la aplicación:

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 la página; test:submit envía una acción con los demás atributos como sus campos; test:expect verifica lo que pasó — el estado, la redirección y el mensaje flash, un texto en la página, filas en una tabla, o el campo en el que se rechazó una entrada. Todo el vocabulario está en la guía Testing an App (en inglés).

Probado: esta página importa los archivos de examples/cookbook/testing/first-test/, y el resultado de arriba es el informe de ejecutarlos.

Licencia MIT · Hecho con VitePress