When writing tests I usually name the modules prefixed with test_
for example spam.py
and test_spam.py
. This makes finding the tests easy. When testing classes in a module I create a unittest.TestCase
derivative with a similar class name, postfixed with Test
. e.g. Spam
becomes SpamTest
(not TestSpam
as this sounds like it is a test implementation of Spam
). Then class functions are tested by test functions that are prefixed with test_
and postfixed with _testcondition
or some other descriptive postfix. I find that this works brilliantly as the original object names are included.
The problem occurs when I want to test module level functions. Following my regular structure I would create a unittest.TestCase
derivative with the same name as the function, postfixed with Test
. The problem with this is that class names are camel cased and function names are lower cased with underscores to separate words. Ignoring the naming convention some_function
becomes SomeFunctionTest
. I cannot help but feeling that this is ugly.
What would be a better fit? What is common practice? Is there a 'standard' like pep8 for this? What do you use?
Unit tests are usually written as a separate code in a different file, and there could be different naming conventions that you could follow. You could either write the name of the unit test file as the name of the code/unit + test separated by an underscore or test + name of the code/unit separated by an underscore.
Naming your tests The name of your test should consist of three parts: The name of the method being tested. The scenario under which it's being tested. The expected behavior when the scenario is invoked.
Test case names are usually limited to a specific length, so space is at a premium. Make sure to keep names unique while refraining from adding any details that aren't required for identification. You can add those details to the case's instructions or test data, for example.
The way you are doing it is the cleanest approach - as long as there is a clear location where people would expect to find the tests for a module level function then I think you are good. The stylistic difference between the function name and the test class name - although an annoyance - isn't significant enough to worry about.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With