前提
CI/CDは初挑戦。
ローカル環境
OS : Windows 11
開発環境 : Spyder (on Anaconda 3)
Python version : 3.9
リポジトリマネージャー
GitLab
ファイル構成
四則演算を実行する関数を実装したsample.py
とそのテストコードを以下のように配置。
gitlab-ci-test #リポジトリ名 └ .gitlab-ci.yml └ calc ├── sample.py └── tests ├── test_sample1.py └── test_sample2.py
ソースコード
CI/CD側でpytestが通る状態のものを最後にまとめて記載。
実現したいこと
GitLabのCI/CDでpytestがパスする状態を維持したままtest_sample1.py
, test_sample2.py
中の冒頭4行のimport文をコメントアウトされている1行で書き直したい。そのために、pthファイルを用いたCI/CDの設定方法が知りたい。
該当のソースコード
pythonファイル
Python:sample.py
1def add(x, y): 2 return x + y 3 4def minus(x, y): 5 return x - y 6 7def multiply(x, y): 8 return x * y 9 10def divide(x, y): 11 return x / y
Python:test_sample1.py
1import os, sys 2current_path = os.path.dirname(os.path.abspath(__file__)) 3sys.path.insert(0, current_path + '/../') 4import sample as funcs 5#本当は以下の1行で済ませたい。 6#from calc import sample as funcs 7 8import pytest 9 10def test_add(): 11 12 assert funcs.add(1,2) == 3 13 14def test_minus(): 15 16 assert funcs.minus(1,2) == -1 17 18 19if __name__ == '__main__': 20 pytest.main()
Python:test_sample2.py
1import os, sys 2current_path = os.path.dirname(os.path.abspath(__file__)) 3sys.path.insert(0, current_path + '/../') 4import sample as funcs 5#本当は以下の1行で済ませたい。 6#from calc import sample as funcs 7 8import pytest 9 10def test_multiply(): 11 12 assert funcs.multiply(1,2) == 2 13 14def test_divide(): 15 16 assert funcs.divide(12,2) == 6 17 18 19if __name__ == '__main__': 20 pytest.main()
ymlファイル
.gitlab-ci.yml
は以下の通り。
[追記]before_script
あたりにpthファイルを配置する命令を書けないか試行錯誤中。
yml
1image: continuumio/anaconda3:latest 2 3before_script: 4 - conda install pytest pytest-cov 5 6stages: # List of stages for jobs, and their order of execution 7 - build 8 - test 9 - deploy 10 11build-job: # This job runs in the build stage, which runs first. 12 stage: build 13 script: 14 - echo "Build process is not defined. Following logs are duumy." 15 - echo "Compiling the code..." 16 - echo "Compile complete." 17 18unit-test-job: # This job runs in the test stage. 19 stage: test # It only starts when the job in the build stage completes successfully. 20 script: 21 - echo "Running unit tests... " 22 - pytest --cov=. 23 - echo "Unit tests finished." 24 25lint-test-job: # This job also runs in the test stage. 26 stage: test # It can run at the same time as unit-test-job (in parallel). 27 script: 28 - echo "Linting code... This will take about 10 seconds." 29 - sleep 10 30 - echo "No lint issues found." 31 32deploy-job: # This job runs in the deploy stage. 33 stage: deploy # It only runs when *both* jobs in the test stage complete successfully. 34 script: 35 - echo "Deploying application..." 36 - echo "Application successfully deployed." 37
ここまでがCI/CDでpytest通る実装。
pthファイル
他のソースファイルはpthファイル未配置前提での記載。
ローカルでpthファイルをC:\Users\[ユーザー名]\anaconda3\envs\[仮想環境名]\lib\site-packages
に配置してtest_sample1.py
, test_sample2.py
の冒頭4行をその下のコメントアウトされている1行に書き換えてもpytestが通ることを確認済み。
hoge.pth
1C:/spyder-workspaces/gitlab-ci-test
あなたの回答
tips
プレビュー