I am trying to write unit tests in Python and trying to find a descriptive way to do something. I have a JavaScript background and I use mocha
, which helps me describe.
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 I am writing unit tests in Python, I get something like this:
foo.spec.py
class TestFoo: def test_when_type_is_undefined(self): ... def test_when_isLogged_is_undefined(self): ...
What is the best way to structure these tests? What are the best practices for descriptive unit testing? Are there any good examples of good unit tests?
javascript python unit-testing
Simon Nov 22 '16 at 14:30 2016-11-22 14:30
source share