<jupyter_start><jupyter_text>**Not proper testing you run into execution errors in code due to software issues you could also be dictating business decisions affect products based on faulty conclusions** * Bad encoding* Inappropriate features* Unexpected featuresTherefore proper testing to be confident about results and to avoid surprises.  Test driven development Writing tests before developing code to implement tasks   Unit tests Test that covers a small unit of code, usually a function  Testing and data science * write unit tests* Tools to improve unit tests* Test specifically for data science  unit test demo Suppose you wanted to test a function that finds the nearest perfect square less than or equal to cetain number.<jupyter_code>from nearest import nearest_square<jupyter_output><empty_output><jupyter_text>Approach 1<jupyter_code>nearest_square(5)
nearest_square(-12)
nearest_square(23)
nearest_square(15)<jupyter_output><empty_output><jupyter_text>It's poor way to test because this isn't repeatable and requires you to type these manually and doesn't tell whether it's returning correct answer. It is inefficient and unreliable way of testing.  Approach 2 you may consider putting test code inside file to make it repeatable<jupyter_code># %load bad_test1.py
#!/usr/bin/env python

from nearest import nearest_square


print("Nearest square <=5:",nearest_square(5))
print("Nearest square <=-12:",nearest_square(-12))
print("Nearest square <=13:",nearest_square(13))
print("Nearest square <=23:",nearest_square(23))<jupyter_output>Nearest square <=5: 4
Nearest square <=-12: 0
Nearest square <=13: 9
Nearest square <=23: 16<jupyter_text>Still have to check manually whether results are correct or not  Approach 3 If we print correct expected result with each answer,now we do have a clear way to check out results. However still have to manually compare what they returned and correct answer yourself.<jupyter_code># %load bad_test2.py
#!/usr/bin/env python


from nearest import nearest_square


print('Nearest square <=5 returned:{},actual answer is 4'.format(nearest_square(5)))
print('Nearest square <=-12 returned:{},actual answer is 0'.format(nearest_square(-12)))
print('Nearest square <=9 returned:{},actual answer is 9'.format(nearest_square(9)))
print('Nearest square <=23 returned:{},actual answer is 16'.format(nearest_square(23)))<jupyter_output>Nearest square <=5 returned:4,actual answer is 4
Nearest square <=-12 returned:0,actual answer is 0
Nearest square <=9 returned:9,actual answer is 9
Nearest square <=23 returned:16,actual answer is 16<jupyter_text>**This is not ideal when we are running large amount of tests**  Approach 4 **We can use assert to ensure that each result is identical to the correct answer.This is much better since it checks  out results automatically.**<jupyter_code># %load bad_test3.py
#!/usr/bin/env python



from nearest import nearest_square

nearest_5=nearest_square(5)
print('Nearest square <=5 returned:{},actual answer is 4'.format(nearest_square))
assert(nearest_5==4)
      
nearest_n12=nearest_square(-12)
print('Nearest square <=-12 returned:{},actual answer is 0'.format(nearest_n12))
assert(nearest_n12==0)
      
nearest_9=nearest_square(9)
print('Nearest square <=9 returned:{},actual answer is 9'.format(nearest_9))
assert(nearest_9==0)
      
nearest_23=nearest_square(23)
print('Nearest square <=23 returned:{},actual answer is 16'.format(nearest_23))
assert(nearest_23==16)<jupyter_output>Nearest square <=5 returned:<function nearest_square at 0x0000012B24045EA0>,actual answer is 4
Nearest square <=-12 returned:0,actual answer is 0
Nearest square <=9 returned:9,actual answer is 9
{'fraction_non_alphanumeric': 0.09053722388873739, 'fraction_numerical': 0.03681483501499864, 'mean_word_length': 6.531827515400411, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '320284', 'license': 'CNRI-Python', 'n_tokens_mistral': 1127, 'n_tokens_neox': 1036, 'n_words': 406, 'path': 'Software_Engineering/Software Engineering Practices Part-2/1.Testing and data science/testing_and_datascience.ipynb', 'repo_name': 'surily/Udacity-Data-Science', 'size': '3667'}