Importance of the tests in software development

Amarnath
2 min readMay 22, 2021

--

Many developers believe writing tests will be time consuming and not worth it but I have a different perception here, yes I agree at the initial time of project development will be time consuming when compared to without writing tests but imagine over time the project becomes bigger and bigger with lots of lines of code and now making any changes to code will be super slow and tedious. Assume the actual developer of the code is unavailable for some reason or moved out of the project and believe me it is a nightmare for any new developers to make even small changes to the existing code.

There are a couple of reasons for this, one they don’t have any idea about the code and second they will be scared to make changes because they don’t know what part of the code will break. Even the actual developer of the code will also face difficulty if he/she sees code after some time. A quotation without tests, At the time of writing code only me and God understands code and after six months only God understands my code :)

Good coverage of test cases will solve these two problems. Tests will act as a documentation for the code to understand and will also give more freedom to make changes thus the development speed has been improved drastically.

Sample test scenarios:

Below code snippets are written in python

  • Below test cases asserts the method return values and expected values
def is_even_number(number: int):
if number % 2 == 0:
return True
return False
def test_is_even_num_method_should_return_true():
expected_data = True
actual_data = is_even_number(4)
assert expected_data == actual_data
def test_is_even_num_method_should_return_false():
expected_data = False
actual_data = is_even_number(5)
assert expected_data == actual_data
  • Below the test cases assert if a method is called or not in another method and if the method throws http error or not.
class GithubAPI:    def get_github_api_response(self):
pass

def get_response(github_gateway: GithubAPI):
return get_github_api_response()
def test_get_response_method_should_call_github_api_method_once():
mock_client = Mock(spec=GithubAPI)
get_response(mock_client)
mock_client.get_github_api_response.assert_called_once()
def test_get_response_should_raise_http_error():
mock_client = Mock(spec=GithubAPI)
mock_client.get_github_api_response.side_effect = HTTPError
with pytest.raises(HTTPError):
get_response(mock_client)

For a complete example click here .

Thanks for reading and please provide your feedback in the comment section if you have and it will be really helpful to me :)

--

--

Amarnath
Amarnath

Written by Amarnath

Passionate Data Engineer, Tech geek | Works at Thoughtworks

No responses yet