Run unittests in Python (for beginners)

Introduction

When I used Python for the first time, I had a hard time before using unittest, so this article is for smooth use. Moreover, it is difficult to start using it unless you understand the basics of Python, so I summarized it.

The directory structure that can be executed successfully is as follows.

.
├─sample.py
└─tests
  ├─__init__.py
  └─test_sample.py

Sample source

sample.py


def add(x, y):
    return x + y

test_sample.py


import unittest
import sample

class TestSample(unittest.TestCase):
    def test_add(self):
        self.assertEqual(sample.add(1, 2), 3)

if __name__ == "__main__":
    unittest.main()

How to run the test

python -m unittest tests.test_sample And unittest scripts

What i learned

I can run the test well with the above method, but I learned the following points.

__init__.What is py? I thought, but the directory where this file is located seems to be treated as a python package. Therefore sample.With the execute command from the directory where py is, "tests.test_"tests" of "sample".It seems that you can specify the package and execute it.



 * Python 2.7 does not recognize it as a package without ``` __ init__.py```, so it is necessary, but it is not necessary because it is recognized as a package even if it is not Python 3.5.

 Also, I am importing with test_sample.py. There are various import search paths, but the execution path is one of them. It seems that you can import with "import sample" by executing from the directory where sample.py is located.

## Experiment
 We are conducting the following experiments to realize what we have learned.

python tests\test_sample.py
 Try running it with.

``` error
Traceback (most recent call last):
  File "tests\test_sample.py", line 2, in <module>
    import sample
ImportError: No module named sample

Error that sample module cannot be found. The execution path seems to be the tests directory, and it seems that the sample in the next higher hierarchy cannot be found.

However, put test_sample.py in the same hierarchy as the sample.py directory, python test_sample.py Will work correctly. python -m unittest test_sample It works correctly here as well.

Restore the directory structure and then delete the `` `__ init__.py``` file python -m unittest tests.test_sample When you run ImportError: No module named tests Error. It doesn't seem to be recognized as a package.

Finally, go to the tests directory and python test_sample.py When you run ImportError: No module named sample Error. python -m unittest test_sample But the same error. In other words, it cannot be imported because there is no sample in the execution directory.

Impressions

Even though I just tried to use unittest in python, I learned a lot about how to specify packages and how to think of directories that can be imported. I learned a lot because I couldn't do it easily. With this, I feel that future python programming can go smoothly.

I want to read it together

How to execute tests collectively with Python unit test

Recommended Posts

Run unittests in Python (for beginners)
Run Python unittests in parallel
python textbook for beginners
OpenCV for Python beginners
[Introduction for beginners] Working with MySQL in Python
Basic story of inheritance in Python (for beginners)
Learning flow for Python beginners
Run automatic jobs in python
Run shell commands in python
Search for strings in Python
Techniques for sorting in Python
Python #function 2 for super beginners
Basic Python grammar for beginners
100 Pandas knocks for Python beginners
Python for super beginners Python #functions 1
~ Tips for beginners to Python ③ ~
About "for _ in range ():" in python
Rock-paper-scissors poi in Python for beginners (answers and explanations)
How to run python in virtual space (for MacOS)
[For beginners] Learn basic Python grammar for free in 5 hours!
Check for memory leaks in Python
Check for external commands in python
Python Exercise for Beginners # 2 [for Statement / While Statement]
Let's run "python -m antigravity" in python
Run shell command / python in R
Python for super beginners Python # dictionary type 1 for super beginners
[For beginners] Summary of standard input in Python (with explanation)
Python #index for super beginners, slices
<For beginners> python library <For machine learning>
Python #len function for super beginners
Beginners use Python for web scraping (1)
Run a simple algorithm in Python
Beginners use Python for web scraping (4) ―― 1
Python #Hello World for super beginners
Python for super beginners Python # dictionary type 2 for super beginners
[For beginners] How to register a library created in Python in PyPI
INSERT into MySQL with Python [For beginners]
[Python] Minutes of study meeting for beginners (7/15)
Notes on nfc.ContactlessFrontend () for nfcpy in python
Inject is recommended for DDD in Python
Tips for dealing with binaries in Python
Summary of various for statements in Python
Type annotations for Python2 in stub files!
Run AzureKinect in Python on Christmas Eve.
Run the Python interpreter in a script
Template for writing batch scripts in python
Process multiple lists with for in Python
MongoDB for the first time in Python
Get a token for conoha in python
Sample for handling eml files in Python
AtCoder cheat sheet in python (for myself)
[Python] Read images with OpenCV (for beginners)
I searched for prime numbers in python
Notes for using python (pydev) in eclipse
WebApi creation with Python (CRUD creation) For beginners
Tips for making small tools in python
Use pathlib in Maya (Python 2.7) for upcoming Python 3.7
Make cron-like jobs run regularly in Python
Atcoder standard input set for beginners (python)
Run Python in C ++ on Visual Studio 2017
[For beginners] Try web scraping with Python