unittest, order of test execution

M

mk

Hello everyone,

I've got 2 functions to test, extrfromfile which returns a list of
dictionaries, and extrvalues that extracts values from that list.

Now I can test them both in one test case, like this:

def test_extrfromfile(self):
valist = ma.extrfromfile('loadavg_unittest.txt')
valist_ut = [ {'day': '08-11-19', 'time': '12:41', 'val': 0.11},
{'day': '08-11-19', 'time': '12:42', 'val': 0.08},
{'day': '08-11-19', 'time': '12:43', 'val': 0.57},
{'day': '08-11-19', 'time': '12:44', 'val': 0.21},
{'day': '08-11-19', 'time': '12:45', 'val': 0.08},
{'day': '08-11-19', 'time': '12:46', 'val': 0.66},
{'day': '08-11-19', 'time': '12:47', 'val': 0.32},
{'day': '08-11-19', 'time': '12:48', 'val': 0.12},
{'day': '08-11-19', 'time': '12:49', 'val': 0.47},
{'day': '08-11-19', 'time': '12:50', 'val': 0.17}]
self.assertEqual(valist, valist_ut)

vlextr_ut = [0.11, 0.08, 0.57, 0.21, 0.08, 0.66, 0.32, 0.12,
0.47, 0.17]
vlextr = ma.extrvalues(valist)
self.assertEqual(len(vlextr_ut), len(vlextr))
for (idx, elem) in enumerate(vlextr_ut):
self.assertAlmostEqual(elem, vlextr[idx])


But I was wondering, *should* this test be separated into two unit
tests, one for each function? On the face of it, it looks that's how it
should be done.

This, however, raises the question: what's the order of test execution
in the unittest? And how to pass values between unit tests? Should I
modify 'self' in unit test?

Regards,
mk
 
Y

Yinon Ehrlich

But I was wondering, *should* this test be separated into two unit
tests, one for each function? On the face of it, it looks that's how it
should be done.

This, however, raises the question: what's the order of test execution
in the unittest? And how to pass values between unit tests? Should I
modify 'self' in unit test?

It's OK to run some tests in the same function.
When one of the asserts fails, following the traceback will lead you
straight to your problem.

The order of test execution is done by default by sorting the test
functions alphabetically.
You may disable the sort by setting self.sortTestMethodsUsing to None.
The simplest way to pass values between tests is to use the class it
(self).

Yinon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top