I'm trying to write unit tests in Python and struggle to find a descriptive way to do things. I have a JavaScript background and I use mocha
which helps me be descriptive.
This is what I mean by "descriptive":
foo.js
exports.foo = function (type, isLogged, iOk) {
if (type === undefined) throw new Error('a cannot be undefined');
if (isLogged === undefined) throw new Error('b cannot be undefined');
if (isLogged) {
if (type === 'WRITER') {
return isOk ? "writer" : -1;
} else {
return "something else"
}
}
}
foo.spec.js
describe('#foo()', function () {
context('when type is undefined', function () {
...
})
context('when isLogged is undefined', function () {
...
})
context('when type is defined', function () {
context('when isLogger is not defined', function () {
...
})
context('when isLogged is defined', function () {
context('when type is not WRITER', function () {
...
})
context('when type is WRITER', function () {
context('when isOk is true', function () {
...
})
})
})
})
})
While when I write unit tests in Python, I end up with somthing like this:
foo.spec.py
class TestFoo:
def test_when_type_is_undefined(self):
...
def test_when_isLogged_is_undefined(self):
...
# This test name is too long
def test_when_type_is_defined_and_isLogged_is_undefined_and_type_is_writer_when_is_ok_is_true(self):
...
How to structure these test in a better way? What are the best practices regarding descriptive unit testing? Are there good examples of good unit tests?