Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unittest tests order

How can I be sure of the unittest methods' order? Is the alphabetical or numeric prefixes the proper way?

class TestFoo(TestCase):     def test_1(self):         ...     def test_2(self):         ... 

or

class TestFoo(TestCase):     def test_a(self):         ...     def test_b(self):         ... 
like image 607
nmb.ten Avatar asked Nov 04 '10 09:11

nmb.ten


People also ask

Does Unittest run tests in order?

Note that 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.

What is the correct order of the unit test layout?

A typical unit test contains 3 phases: First, it initializes a small piece of an application it wants to test (also known as the system under test, or SUT), then it applies some stimulus to the system under test (usually by calling a method on it), and finally, it observes the resulting behavior.

What is the correct order of execution in JUnit?

By default, JUnit runs tests using a deterministic but unpredictable order (MethodSorters. DEFAULT). In most cases, that behavior is perfectly fine and acceptable. But there are cases when we need to enforce a specific ordering.


1 Answers

You can disable it by setting sortTestMethodsUsing to None:

import unittest unittest.TestLoader.sortTestMethodsUsing = None 

For pure unit tests, you folks are right; but for component tests and integration tests... I do not agree that you shall assume nothing about the state. What if you are testing the state?

For example, your test validates that a service is auto-started upon installation. If in your setup, you start the service, then do the assertion, and then you are no longer testing the state, but you are testing the "service start" functionality.

Another example is when your setup takes a long time or requires a lot of space and it just becomes impractical to run the setup frequently.

Many developers tend to use "unit test" frameworks for component testing...so stop and ask yourself, am I doing unit testing or component testing?

like image 50
max Avatar answered Sep 21 '22 07:09

max