Saltar al contenido

Pruebas de Python

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

En esta página
Open in ChatGPTOpen in Claude

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:

# 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

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

unittest

unittest viene con la biblioteca estándar:

# 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!")
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:

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


def test_default_name():
    assert greeting("world") == "Hello, world!"
VIRTUAL_ENV=.venv elide python -m pytest -q

pytest recopila ambos archivos y termina con:

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?