以下の公式ドキュメントからのコードを参考に二番目のコードの箇所をconftest.pyにまとめて書いて、どのテストでも動くようにしたいです。
自分であれば、こういうふうに書きますやこういうふうにかけますよなど、ご教示いただけませんでしょうか?
また、以下のような記事をたくさんみていますが、どういう処理かは理解できますし、簡単なコードに対しての使い方は理解できますが、本質的な部分(どれだけ便利でメリット・デメリット及び具体的な使い方)が私の知見ではよく理解できていません。
https://qiita.com/mitamura_kei/items/e51c604155633ccd33dd
また、以下のコードは公式ドキュメントからの抜粋ですが、コマンドラインオプションを使ったテストコードを書く機会はそこまでない気がしていて、現状どう参考にすれば困っている状況です。https://docs.pytest.org/en/latest/example/simple.html
上記、使う機会があるよ、などこういうときに使うよ、などもご存じでしたら教えて頂けますと助かります。
Suppose we want to write a test that depends on a command line option. Here is a basic pattern to achieve this: # content of test_sample.py def test_answer(cmdopt): if cmdopt == "type1": print("first") elif cmdopt == "type2": print("second") assert 0 # to see what was printed For this to work we need to add a command line option and provide the cmdopt through a fixture function: # content of conftest.py import pytest def pytest_addoption(parser): parser.addoption( "--cmdopt", action="store", default="type1", help="my option: type1 or type2" ) @pytest.fixture def cmdopt(request): return request.config.getoption("--cmdopt")
前処理コードとしてconftest.pyにまとめたいコード。
from xxx.xxx.db import dba ... ... ... def create_app(): app = Flask(__name__) app.config.from_object(test) dba.init_app(app) logger = logging.getLogger(__name__) init_app(app, dba, logger) return app app = create_app() app.app_context().push() url = 'http://localhost:8080/batch' usrPass = "xxxx" headers = {'Authorization': 'Bearer %s' % usrPass, 'Content-Type': 'application/json'} payload = {"requests": [{"method": "post", "relative_url": "/acs", "body": {"exuser": "test1"}}, { "method": "post", "relative_url": "/acs", "body": {"exuser": "test2"}}, {"method": "post", "relative_url": "/acs", "body": {"exuser": "test3"}}]} with app.test_request_context(url, method="POST") as req: req.request.args = payload req.request.headers = headers r = req.request
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/18 12:17
2018/07/18 12:59
2018/07/18 13:03 編集
2018/07/18 15:00
2018/07/18 15:08 編集
2018/07/18 15:27