Filosofía y orden de la prueba de unidad de Python

While the unittesting philosophy is that tests can be ran in any order and it should pass, what if you're implementing an API where there is no other means of communicating with a server... and you need to test a certain very basic feature (such as delete) before you can do more complicated tasks? Is ordering the tests then reasonable?

If so, how can I do it with python's unittest ¿módulo?

preguntado el 09 de marzo de 12 a las 16:03

To clarify: you're saying you want to run the delete test first, because there's no point running the others if it fails? -

Even if those first tests fail, the later tests with delete will still be run, so what's the problem? -

In broadbrush strokes, "If your unit tests have dependencies on external services or other unit tests, they're not unit tests." Sounds a little dogmatic. But it's a big red flag. If you're testing complex dependent semantics of the service itself then you likely want to encapsulate all of those 'steps' in a single test. But that becomes protocol testing. Unit tests really should be independent. -

@ultimatebuster: What you're trying to do here is some kind of integration test, not a unit test. -

1 Respuestas

You already seem to realise that your unit tests should be independent. The only other reason I can see that you want to run the tests in some fixed order is that you want to stop running the suite if an early test fails. To do that, you can use the opción de línea de comandos

-f, --failfast

Detenga la ejecución de prueba en el primer error o falla.

By the way, the tests are run in alphabetical order:

the order in which the various test cases will be run is determined by sorting the test function names with respect to the built-in ordering for strings.


respondido 09 mar '12, 17:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.