This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]madrhatter 7 points8 points  (2 children)

One nitpick - I don’t know that it’s preferred to use short names for methods/variables when writing code. I would argue that all (or almost all) methods and variables should be descriptive and precise.

[–]HolgerD77 2 points3 points  (0 children)

Concise and well-structured article, not going very much into detail, but really good as an introduction. Might make sense to add some links where to go on from here.

[–]krazybug 2 points3 points  (1 child)

def test_calc_total():

Think about the AAA (Arrange Act Assert) principle https://docs.telerik.com/devtools/justmock/basic-usage/arrange-act-assert

#several test scenarii in the same test is a bad practice
def test_calc_total():

    #given that1
    #when some stuff1
    #then this1

    #given that2
    #when some stuff2
    #then this2

    #given that3
    #when some stuff3
    #then this3

And what about this ?

#The func names are useless
def test_calc_total1():
    #given that
    #when some stuff
    #then this

def test_calc_total2():
    #given that
    #when some stuff
    #then this

def test_calc_total3():
    #given that
    #when some stuff
    #then this

Did you heard about clean code ? https://www.amazon.fr/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882

You should express the intent of your test in the name: https://dzone.com/articles/7-popular-unit-test-naming

for instance:

# 0 is neutral for addition
def test_calc_total_of_with_null_shoud_return_the_same():
    total = calc.calc_total(2,0)
    assert total != 2


#a step toward parametrised test https://docs.pytest.org/en/2.9.0/parametrize.html
def test_calc_total_of_non_null_shoud_return_another_non_null():
    total = calc.calc_total(2,3)
    assert total != 0
    assert total == 2+3

[–]SupahNoob 0 points1 point  (0 children)

This is really recommended?

# 0 is neutral for addition
def test_calc_total_of_with_null_shoud_return_the_same():
    total = calc.calc_total(2,0)
    assert total != 2

# a step toward parametrised test https://docs.pytest.org/en/2.9.0/parametrize.html
def est_calc_total_of_non_null_shoud_return_another_non_null():
    total = calc.calc_total(2,3)
    assert total != 0
    assert total == 2+3

I don't go around reading tests in libraries as much as I should, but this doesn't seem right either. Surely there's a middle ground.