Isn't it okay to write test code?

This article "has never written" test code, "doesn't need", "doesn't understand" This is an article that introduces people such as "There is something good to write."

I don't know how to write in the first place

Have you learned how to write test code? Speaking of which, you're doing something difficult, right? I feel that there are a certain number of people. First of all, the conclusion is that the test code is basically ** It's rather muddy! ** **

Somehow it automatically pulls the function in the source file, It can also be a value evaluation. But it ’s difficult, right?

I think there are people who think that. There was a time when I thought so. (I think you can find it, but it probably costs money. Rather, you can get money)

To give a simple example, A function to get the bark of a dog by specifying a country

dog_bark.py


def dog_bark(country):
  if country == 'Japan':
    return u'Bow-wow!'
  elif country == 'USA':
    return u'Bow wow!'
  else:
    raise NotImplementedError

Test code is

test_dog_bark.py


from dog_bark import dog_bark

def test_dog_bark():
  assert u'Bow-wow!' == dog_bark('Japan')
  assert u'Bow wow!' == dog_bark('USA')

It will be. The code is to actually execute the dog_bark function and compare it directly with the expected result string. Let's write a test code to tell the world! Let's mass-produce the code like this! It means that. It's not particularly difficult. Test execution is, for example, Python's Pytest

$ py.test test_dog_bark.py


 Is OK. Even if you increase the test function, it will recognize the test in the file without permission.
 Will do it.
 By the way, any test framework in any language has this much functionality.
 (Language with compilation takes a little more work)

## It's a pain to write a test
 I understand how to write a test, but "after all, just start the app and check"
 That's right. However,

 ――When will the app be ready to launch?
 ――When will you be able to check the function in the app?
 ――Assuming that you can check it in the app, every time you fix the code, you can check the app
 Do you restart, move the screen, manually enter the parameters, and visually confirm?

 ** The easiest deshow is to type a one-line command on the command line and have your PC test it for you! ?? ** **

 That's right. Checking in the actual app is much more troublesome if you increase the number of times.
 I think the claim for quality is certainly correct, but here I dare to
 ** Let's write a test to enjoy the quality! ** **
 Insist. It doesn't matter if you are highly conscious when writing a test. (Rogue)
 Programmers have a hard time trying to have fun, and there is also the word Nambo.

 ~~ Who guarantees the quality of the test and the correctness of the test itself ... ~~

 In addition to that, it can be considered complete until the test passes, so
 ** Get a sense of progress **
 Because it is quite important.
 ~~ We promise that it will be better to throw it away after all due to specification changes ~~

## You will also have design skills
 Even if you try to write a test during development
 Necessary related functions are not implemented, or master data is not yet available
 There is a situation like that. Do you want to wait until you can?

 ** You can write it if you divide the module well or use a mock **

 In addition, the test written in this way

 ** A constantly moving test independent of other modules and fluctuating master data **
 It will be. This is important.
 It's easy to see the extent of the impact of bug fixes and enhancements.

 Of course, it takes some learning and experience to divide modules, etc.
 I think that whether or not you are aware of it has a great influence on the overall product.
 (After all, the story of high consciousness)

## In summary, let's do TDD!
 In summary
 
 -** It is easier to check the operation with the test code **
 -** The design looks good **

 Or rather, these are usually TDD.
 ** In the first place, there is no purpose to write a test on TDD to improve quality **.
 Since the test remains as a side effect, the quality will improve if you use it for regression etc.
 It is a retrofit.
 However, I personally wonder if both cats and scoops are TDD, so for the time being,
 ** Have fun with the test code! ** **
 In summary.


Recommended Posts

Isn't it okay to write test code?
Write code to Unit Test a Python web app
Write selenium test code in python
Qiita (1) How to write a code name
Write python-like code
chmod 600 ← 600 isn't it?
I want to write in Python! (1) Code format check
I want to write in Python! (2) Let's write a test
Write Python-like code (dictionary)
Strengthen with code test ⑦
Strengthen with code test ⑨
Strengthen with code test ③
Rewrite Python2 code to Python3 (2to3)
Strengthen with code test ⑤
Strengthen with code test ④
Strengthen with code test ②
[Python / Ruby] Understanding with code How to get data from online and write it to CSV
[Memo] Test code summary
Strengthen with code test ①
Strengthen with code test ⑧
Strengthen with code test ⑨
Test code to check for broken links in the page
How to write a test for processing that uses BigQuery
To write a test in Go, first design the interface
How doi may be useful when asking how to write code?
If you write go table driven test in python, it may be better to use subTest