reading-notes

Software Development Reading Notes

View on GitHub

In Tests We Trust — TDD with Python

Note Source: https://code.likeagirl.io/in-tests-we-trust-tdd-with-python-af69f47e6932

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.

The Cycle

Takeaways

What does the if name == “main”: do?

Note source: https://www.geeksforgeeks.org/what-does-the-if-__name__-main-do/

# 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