Set python test in jenkins

Regarding unit test

Preparation

Install unittest. (Python settings are omitted)

pip install unittest

Unittest example:

test.py


import unittest

class TestTagAprioriMain(unittest.TestCase):

    def setUp(self):
        print('start')

    def tearDown(self):
        print('finished')

    def test_success(self):
        res = 1 + 2
        self.assertEqual(res, 3)

    def test_success2(self):
        res = 1 + 2
        self.assertEqual(res, 3)

    def this_is_ignored(self):
        res = 1 + 2
        self.assertEqual(res, 1)

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

Execution result


start
finished
.start
finished
.
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

The only point I want to suppress:

  1. Methods starting with test_ are subject to testing. (In the above example, this_is_ignored is not called, it is ran 2 tests.)
  2. setUp and tearDown are called before the test (test_ <..>) method, respectively. (It outputs start and finished to make it easier to understand when it is called)
  3. This time, I used self.assertEqual () to check if the two are the same, but there are many others, so use the appropriate one.

How to test

Basic things:

self.assertEqual() 
self.assertTrue()
self.assertFalse()

Error checking

There is also an error test, and you can write the error pattern concretely by writing as follows. (The one on the official page below is posted as it is)

s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
with self.assertRaises(TypeError):
    s.split(2)

Official document:

python3: https://docs.python.org/3/library/unittest.html python2.7: https://docs.python.org/2.7/library/unittest.html

About jenkins

What is jenkins

https://jenkins.io/ jenkins is well known as a CI (Continuous Integration) tool. What is "continuous integration" just by looking at this? May be, but

Wikipedia:

http://e-words.jp/w/%E7%B6%99%E7%B6%9A%E7%9A%84%E3%82%A4%E3%83%B3%E3%83%86%E3%82%B0%E3%83%AC%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3.html :

In other words, if you think about it simply, it's like ** continuously passing through tests **. (Because the author doesn't fully understand it)

The image is something like "automatically, you can check that the test has passed regularly".

Regarding the introduction of Jenkins

See other pages as we won't cover this time: https://appkitbox.com/knowledge/test/20121112-116

Make a new item

Set as Bitbucket has Repo

  1. Set the git repo to bitbucket and register the SSH PubKey in Bitbucket as well.
  2. Check Build when a change is pushed to BitBucket.

python version settings

If you don't have the pyenv build wrapper, install it on Jenkins

  1. Select pyenv build wrapper in Build Environment and set 3.4.1 this time
  2. Write the required Library in the preinstall pip list. Example: numpy, pyyaml, pandas

Set what works in the test

Write the necessary variable settings and commands to run in the execute shell of Build. (The following may be included, but ...)

PYENV_HOME=$WORKSPACE/.pyenv/
export PYTHONPATH=$WORKSPACE:"$(dirname "$WORKSPACE")"
python setup.py test

That's it. After that, save your changes, build and make sure it's a success.

If the test fails,

From the Job number, if you look at the Output Console, you can see the error just like a normal Terminal.

Recommended Posts

Set python test in jenkins
Set up a test SMTP server in Python.
Algorithm in Python (primality test)
Write selenium test code in python
Quadtree in Python --2
Python in optimization
CURL in python
Python set arithmetic
Metaprogramming in Python
Python 3.3 in Anaconda
SendKeys in Python
Stress Test with Locust written in Python
Write the test in a python docstring
Python set arithmetic
To set default encoding to utf-8 in python
Epoch in Python
Discord in Python
Post Test 3 (Working with PosgreSQL in Python)
Sudoku in Python
DCI in Python
quicksort in python
Associate the table set in python models.py
nCr in python
N-Gram in Python
Programming in python
Constant in python
Python Integrity Test
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Set up a simple HTTPS server in Python 3
Create a Vim + Python test environment in 1 minute
I want to do Dunnett's test in Python
Statistical test grade 2 probability distribution learned in Python ②
Prime number enumeration and primality test in Python
Set up a simple SMTP server in Python
Statistical test grade 2 probability distribution learned in Python ①
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python