In Tests We Trust — TDD with Python
Note Source: https://code.likeagirl.io/in-tests-we-trust-tdd-with-python-af69f47e6932
- Test-Driven Development is a strategy to think (and write!) tests first.
A sample TDD test code:
def test_should_return_female_when_the_name_is_from_female_gender():
detector = GenderDetector()
expected_gender = detector.run(‘Ana’)
assert expected_gender == ‘female’
Standard Python project tree list:
mymodule/
— module.py
— another_folder/
— — another_module.py
tests/
— test_module.py
— another_folder/
— — test_another_module.py
### AAA: Arrange, Act and Assert.
- Arrange: you need to organize the data needed to execute that piece of code (input);
- Act: here you will execute the code being tested (exercise the behaviour);
- Assert: after executing the code, you will check if the result (output) is the same as you were expecting.
The Cycle
- 🆘 Write a unit test and make it fail (it needs to fail because the feature isn’t there, right? If this test passes, call the Ghostbusters, really)
- ✅ Write the feature and make the test pass! (you can dance after that)
- 🔵 Refactor the code — the first version doesn’t need to be the beautiful one (don’t be shy)
Takeaways
- The greatest advantage about TDD is to craft the software design first
- Your code will be more reliable: after a change you can run your tests and be in peace
- Beginning may be hard — and that’s fine. You just need to practice!
What does the if name == “main”: do?
Note source: https://www.geeksforgeeks.org/what-does-the-if-__name__-main-do/
- Every Python module has it’s name defined and if this is ‘main’, it implies that the module is being run standalone by the user and we can do corresponding appropriate actions.
- If you import this script as a module in another script, the name is set to the name of the script/module.
- Python files can act as either reusable modules, or as standalone programs.
- if name == “main”: is used to execute some code only if the file was run directly, and not imported.
# Python program to execute
# function directly
def my_function():
print ("I am inside function")
# We can test function by calling it.
my_function()
if we want to use that module by importing we have to comment out our call. Rather than that approach best approach is to use following code:
# Python program to use
# main for function call.
if __name__ == "__main__":
my_function()
import myscript
myscript.my_function()
Recursion
The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function.
A recursive function solves a particular problem by calling a copy of itself and solving smaller subproblems of the original problems. Many more recursive calls can be generated as and when required. It is essential to know that we should provide a certain case in order to terminate this recursion process.
# A Python 3 program to
# demonstrate working of
# recursion
def printFun(test):
if (test < 1):
return
else:
print(test, end=" ")
printFun(test-1) # statement 2
print(test, end=" ")
return
# Driver Code
test = 3
printFun(test)
# This code is contributed by
# Smitha Dinesh Semwal