[Python] Super easy test with assert statement

I didn't have much basic information, so make a note.

What is an assert statement?

Throws an exception when the conditional expression is not True. If you prepare this, you will be able to quickly notice when the code that worked properly until then suddenly behaves differently than expected while you are playing with it. If you just need to say "stop if it's not what you expected", it's convenient because you don't have to write a test class such as unittest. In ** machine learning and data analysis **, there are many parts where specifications have not been decided and trial and error is required, and there are quite a few ad hoc responses, so I think that writing tests is not always familiar. .. I would especially like to recommend it in such areas.

How to use

The syntax is as follows:

assert conditional expression,Message to be output when the conditional expression is False

If the conditional expression is `False```, then the exception ```AssertionError``` is thrown. If the conditional expression is True ``, nothing happens.

>>> kitai = 100
>>> input = 1
>>> assert kitai == input, 'Expected value[{0}],Input value[{1}]'.format(kitai, input)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError:Expected value[100],Input value[1]

If you handle exceptions properly, it looks like this.

>>> try:
...     kitai = 100
...     input = 1
...     assert kitai == input, 'Expected value[{0}],Input value[{1}]'.format(kitai, input)
... except AssertionError as err:
...     print('AssertionError :', err)
... 

AssertionError :Expected value[100],Input value[1]

Disable assert statement

The assert statement is executed only when the built-in constant `__debug__``` is True```. This is the state if nothing is done. Adding `` -O``` to the command line option when running a Python script will change `` debug to `` `False and invalidate the assert statement.

reference

Python3.6 official documentation -7.3. assert statement -3. Built-in constant __debug__ -1. Command line and environment -O option

Recommended Posts

[Python] Super easy test with assert statement
Python assert statement
Primality test with Python
Primality test with python
[Co-occurrence analysis] Easy co-occurrence analysis with Python! [Python]
Easy folder synchronization with Python
Addition with Python if statement
Easy Python compilation with NUITKA-Utilities
Easy HTTP server with Python
[Super easy] Let's make a LINE BOT with Python.
[Python] What is a with statement?
[Python] Easy parallel processing with Joblib
Easy Python + OpenCV programming with Canopy
Unit test log output with python
Easy email sending with haste python3
Bayesian optimization very easy with Python
Easy data visualization with Python seaborn.
Easy parallel execution with python subprocess
Easy modeling with Blender and Python
Easy keyword extraction with TermExtract for Python
Stress Test with Locust written in Python
Test Python non-functional programs with GitLab CI
[Python] Easy argument type check with dataclass
WebUI test with Python2.6 + Selenium 2.44.0 --profile setting
Generate Japanese test data with Python faker
Make GUI apps super easy with tkinter
Easy introduction of speech recognition with Python
Post Test 3 (Working with PosgreSQL in Python)
How to do portmanteau test with python
[Easy Python] Reading Excel files with openpyxl
Integrating with setuptools / python setup.py test / pytest-runner
Easy web app with Python + Flask + Heroku
Easy image processing in Python with Pillow
[Easy Python] Reading Excel files with pandas
Easy web scraping with Python and Ruby
[Python] Easy Reinforcement Learning (DQN) with Keras-RL
Python if statement
FizzBuzz with Python3
Csv output from Google search with [Python]! 【Easy】
Scraping with Python
Create test data like that with Python (Part 1)
Python is easy
Statistics with python
Python for super beginners Python for super beginners # Easy to get angry
Scraping with Python
Python with Go
Twilio with Python
Python with Progate (dictionary, while statement, break, continue)
Integrate with Python
Play with 2016-Python
I tried super easy linear separation with Chainer
AES256 with python
Tested with Python
python starts with ()
Super easy! Python + Flask environment in Docker quickly
Generate an insert statement from CSV with Python.
Easy understanding of Python for & arrays (for super beginners)
with syntax (Python)
Python Integrity Test
Python exec statement
Bingo with python