質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

GitLab

GitLabは Gitoliteをブラウザから管理できるようにする Rubyアプリケーションで、 GitHubのようなサービスをクローズドな環境に独自で構築できるように 公開されたものです。

CI(継続的インテグレーション)

CI(継続的インテグレーション)は、アプリ開発においてビルドとテストを繰り返すことで品質改善と納期短縮を図る手法です。JenkinsやTravis CIなどの専用ツールを利用してプロセスを自動化・半自動化して効率的に実施します。

Q&A

0回答

508閲覧

GitLabのCI/CDでpthファイルを配置してpytestを実行したい

throwsNullPo

総合スコア17

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

GitLab

GitLabは Gitoliteをブラウザから管理できるようにする Rubyアプリケーションで、 GitHubのようなサービスをクローズドな環境に独自で構築できるように 公開されたものです。

CI(継続的インテグレーション)

CI(継続的インテグレーション)は、アプリ開発においてビルドとテストを繰り返すことで品質改善と納期短縮を図る手法です。JenkinsやTravis CIなどの専用ツールを利用してプロセスを自動化・半自動化して効率的に実施します。

0グッド

0クリップ

投稿2022/07/03 15:51

編集2022/07/04 02:03

前提

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

y_waiwai

2022/07/03 22:58

現状ではどういった不具合があって、それをどう言う風にしたいんでしょうか
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問