Saltar al contenido

Pruebas de Python

Ejecute pruebas de Python en Elide con unittest de la biblioteca estándar o pytest desde un entorno virtual; elide test no descubre pruebas de Python en 1.6.0.

Leer sin conexiónelide help --lang es python-testing

En esta página

elide test no descubre pruebas de Python en 1.6.0; en un directorio solo con Python imprime No tests found. y termina con 0. En su lugar, lance el ejecutor de pruebas como módulo. El código de salida es el del ejecutor, así que una prueba fallida hace fallar el comando.

Los ejemplos prueban este módulo:

python
# greet/cli.py
import sys


def greeting(name: str) -> str:
    return f"Hello, {name}!"


def main() -> int:
    print(greeting(sys.argv[1] if len(sys.argv) > 1 else "world"))
    return 0
utf-8 · python · Líneas: 11

greet/ y tests/ contienen cada uno un __init__.py vacío.

unittest

unittest viene con la biblioteca estándar:

python
# tests/test_cli.py
import unittest

from greet.cli import greeting


class GreetingTest(unittest.TestCase):
    def test_names_the_caller(self):
        self.assertEqual(greeting("Elide"), "Hello, Elide!")
utf-8 · python · Líneas: 9
Terminal
bash
elide python -m unittest discover -s tests -t .
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK

pytest

Instale pytest en un entorno virtual de Python 3.13 (Paquetes y entornos virtuales) y ejecútelo con VIRTUAL_ENV definido:

Terminal
bash
uv venv --python 3.13 .venv
uv pip install --python .venv/bin/python pytest
python
# tests/test_greeting.py
from greet.cli import greeting


def test_default_name():
    assert greeting("world") == "Hello, world!"
utf-8 · python · Líneas: 6
Terminal
bash
VIRTUAL_ENV=.venv elide python -m pytest -q

pytest recopila ambos archivos y termina con:

Terminal
2 passed in 0.03s

En CI

Añada el componente de Python después de instalar Elide (elide setup python) y ejecute los mismos comandos; consulte Integración continua.

¿Te resultó útil esta página?