1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Git commit するときにUnitTestを自動的に行う方法

Posted at

#結論

.git/hooks/pre-commitにUnittestを実行するよう記述

.git/hooks/pre-commit

#!/bin/sh
python -m unittest

#お試し方法

  • git環境を作成
  • hogehoge.pyとtest.pyを作成
  • テストが適切に動作することを確認
  • .git/hooks/pre-commitに記述
  • git commitをして動作するか確認

git環境を作成

git

git init

hogehoge.pyとtest.pyを作成

hogehoge.py

def add(a, b):
    return a + b


if __name__ == "__main__":
    i = add(1, 2)
    print(i)
test.py
import unittest
import hogehoge


class Test(unittest.TestCase):
    def test(self):
        i = hogehoge.add(1, 2)
        self.assertEqual(i, 3)


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

テストが適切に動作することを確認

>python test.py
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK

.git/hooks/pre-commitに記述

.git/hooks/pre-commit

#!/bin/sh
#pythonの仮想環境に切替
python -m unittest

git commitをして動作するか確認


git commit -m "sample"
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
[master f422770] sample
 1 file changed, 1 insertion(+), 1 deletion(-)

参考資料

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?