I am a heavy, heavy, user of diffing-approaches to testing, so I tend to feed complex multi-line text strings to unittest.assertEqual
. Typical of this type of use would be feeding a big chunk of html, which I will typically pre-process with beautifulsoup.prettify
What I have found is that, when the strings are sufficiently big and sufficiently different , assertEqual
will spend either spend a huge amount of time, or just plain not return. If I ctrl-c the process, I will often find that it was interrupted in difflib.py
. Sometimes, a pkill -laf 'ython.+test'
in another terminal is needed.
Is that a simple Python approach that would me to conceptually execute:
unittest.assertEqual(a,b, timeout=5)
where that means after 5 seconds, terminate and tell me they're not equal and you couldn't get more details .
Of course, I assume that if I pass in the assertEqual.msg
parameter, the function will not call difflib.py. Interestingly, while the diff
command line utility typically returns a result after less than a second, a less clever differ, like Kaleidoscope on macOS, will often hang on those diffs as well.
Ideally, https://pypi.org/project/func-timeout/ would do exactly what I want. But it's LGPL, so not an option for my MIT application.
I already have some similar, thread-based, code to func_timeout
, used to terminate an SQL connection attempt after a suitable delay. Guess I could use that. I could also launch a subprocess.popen(diff,..., timeout=5)
to call the OS's native diff
, since my strings are typically saved to file as well.
Seems to me like it's a problem that people pushing assertEqual a bit would frequently encounter, so wondering what approaches people have taken.
您可以通过使用unittest.assertTrue(a == b, "strings not equal")
来解决整个问题,避免使用diff。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.