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

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

新規登録して質問してみよう
ただいま回答率
85.46%
Flask

FlaskはPython用のマイクロフレームワークであり、Werkzeug・Jinja 2・good intentionsをベースにしています。

Webサイト

一つのドメイン上に存在するWebページの集合体をWebサイトと呼びます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Q&A

解決済

1回答

1306閲覧

Flask公式チュートリアルの「pytest」がうまくいかない

yosse95ai

総合スコア39

Flask

FlaskはPython用のマイクロフレームワークであり、Werkzeug・Jinja 2・good intentionsをベースにしています。

Webサイト

一つのドメイン上に存在するWebページの集合体をWebサイトと呼びます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

0グッド

0クリップ

投稿2020/09/17 08:43

編集2020/10/08 17:21

以下拙い文章ですがどうかよろしくお願いします

前提・実現したいこと

Flask公式チュートリアルをしていたのですが、「pytest」をするところで発生するエラーによって次の項に進めていません。
最初自分でコードをタイプしていたのですが、エラーが出たためいったんコピペしなおしました。

発生している問題・エラーメッセージ

cmd

1(venv) C:\flask-tutorial>flask init-db 2Initialized the database. 3 4(venv) C:\flask-tutorial>pytest 5========================================================= test session starts ========================================================= 6platform win32 -- Python 3.6.8, pytest-6.0.2, py-1.9.0, pluggy-0.13.1 7rootdir: C:flask-tutorial, configfile: setup.cfg, testpaths: tests 8collected 24 items 9 10tests\test_auth.py ........ [ 33%] 11tests\test_blog.py F........... [ 83%] 12tests\test_db.py .. [ 91%] 13tests\test_factory.py .. [100%] 14 15============================================================== FAILURES =============================================================== 16_____________________________________________________________ test_index ______________________________________________________________ 17 18client = <FlaskClient <Flask 'flaskr'>>, auth = <conftest.AuthActions object at 0x000001E96D9C8D30> 19 20 def test_index(client, auth): 21 response = client.get("/") 22 assert b"Log In" in response.data 23 assert b"Register" in response.data 24 25 auth.login() 26 response = client.get("/") 27 assert b"test title" in response.data 28> assert b"by test on 2018-01-01" in response.data 29E assert b'by test on 2018-01-01' in b'<!doctype html>\n<title>\n Posts\n - Flaskr</title>\n<link rel="stylesheet" href="/static/style.css">\n<nav>\n <h...e">Edit</a>\n \n </header>\n <p class="body">test\nbody</p>\n </article>\n \n \n\n 30</section>' 31E + where b'<!doctype html>\n<title>\n Posts\n - Flaskr</title>\n<link rel="stylesheet" href="/static/style.css">\n<nav>\n <h...e">Edit</a>\n \n </header>\n <p class="body">test\nbody</p>\n </article>\n \n \n\n </section>' = <Response 789 bytes [200 OK]>.data 32 33tests\test_blog.py:14: AssertionError 34======================================================= short test summary info ======================================================= 35FAILED tests/test_blog.py::test_index - assert b'by test on 2018-01-01' in b'<!doctype html>\n<title>\n Posts\n - Flaskr</title>\n<... 36==================================================== 1 failed, 23 passed in 3.70s ===================================================== 37 38

エラーが横長で見にくくてすみません。

該当のソースコード

どうやら test_blog.pytest_index関数でエラーしていますので
そちらを張らせていただきます。

もし、追加で提示した方がいいコードなどがありましたら教えていただきたいです。

python

1import pytest 2 3from flaskr.db import get_db 4 5 6def test_index(client, auth): 7 response = client.get("/") 8 assert b"Log In" in response.data 9 assert b"Register" in response.data 10 11 auth.login() 12 response = client.get("/") 13 assert b"test title" in response.data 14 assert b"by test on 2018-01-01" in response.data 15 assert b"test\nbody" in response.data 16 assert b'href="/1/update"' in response.data 17 18 19@pytest.mark.parametrize("path", ("/create", "/1/update", "/1/delete")) 20def test_login_required(client, path): 21 response = client.post(path) 22 assert response.headers["Location"] == "http://localhost/auth/login" 23 24 25def test_author_required(app, client, auth): 26 # change the post author to another user 27 with app.app_context(): 28 db = get_db() 29 db.execute("UPDATE post SET author_id = 2 WHERE id = 1") 30 db.commit() 31 32 auth.login() 33 # current user can't modify other user's post 34 assert client.post("/1/update").status_code == 403 35 assert client.post("/1/delete").status_code == 403 36 # current user doesn't see edit link 37 assert b'href="/1/update"' not in client.get("/").data 38 39 40@pytest.mark.parametrize("path", ("/2/update", "/2/delete")) 41def test_exists_required(client, auth, path): 42 auth.login() 43 assert client.post(path).status_code == 404 44 45 46def test_create(client, auth, app): 47 auth.login() 48 assert client.get("/create").status_code == 200 49 client.post("/create", data={"title": "created", "body": ""}) 50 51 with app.app_context(): 52 db = get_db() 53 count = db.execute("SELECT COUNT(id) FROM post").fetchone()[0] 54 assert count == 2 55 56 57def test_update(client, auth, app): 58 auth.login() 59 assert client.get("/1/update").status_code == 200 60 client.post("/1/update", data={"title": "updated", "body": ""}) 61 62 with app.app_context(): 63 db = get_db() 64 post = db.execute("SELECT * FROM post WHERE id = 1").fetchone() 65 assert post["title"] == "updated" 66 67 68@pytest.mark.parametrize("path", ("/create", "/1/update")) 69def test_create_update_validate(client, auth, path): 70 auth.login() 71 response = client.post(path, data={"title": "", "body": ""}) 72 assert b"Title is required." in response.data 73 74 75def test_delete(client, auth, app): 76 auth.login() 77 response = client.post("/1/delete") 78 assert response.headers["Location"] == "http://localhost/" 79 80 with app.app_context(): 81 db = get_db() 82 post = db.execute("SELECT * FROM post WHERE id = 1").fetchone() 83 assert post is None 84

また使用しているSQLの内容は以下の通りです。

sql

1INSERT INTO user (username, password) 2VALUES 3 ('test', 'pbkdf2:sha256:50000$TCI4GzcX$0de171a4f4dac32e3364c7ddc7c14f3e2fa61f2d17574483f7ffbb431b4acb2f'), 4 ('other', 'pbkdf2:sha256:50000$kJPKsz6N$d2d4784f1b030a9761f5ccaeeaca413f27f2ecb76d6168407af962ddce849f79'); 5 6INSERT INTO post (title, body, author_id, created) 7VALUES 8 ('test title', 'test' || x'0a' || 'body', 1, '2018-01-01 00:00:00');

試したこと

一度コピペして前項の「インストール可能なプロジェクト」をやり直しました。

その後、pytest や coverageを試しなおしました。

補足情報(FW/ツールのバージョンなど)

pip したものは以下のようになっています。

cmd

1(venv) C:\flask-tutorial>pip list 2Package Version Location 3------------------ ------- -------------------------------------------------------------- 4atomicwrites 1.4.0 5attrs 20.2.0 6click 7.1.2 7colorama 0.4.3 8coverage 5.3 9Flask 1.1.2 10flaskr 1.0.0 c:\flask-tutorial 11importlib-metadata 1.7.0 12iniconfig 1.0.1 13itsdangerous 1.1.0 14Jinja2 2.11.2 15MarkupSafe 1.1.1 16more-itertools 8.5.0 17packaging 20.4 18pip 20.2.3 19pluggy 0.13.1 20py 1.9.0 21pyparsing 2.4.7 22pytest 6.0.2 23setuptools 40.6.2 24six 1.15.0 25toml 0.10.1 26Werkzeug 1.0.1 27zipp 3.1.0

私の質問の仕方が悪く、わかりにくいところ等ございましたら教えていただけると幸いです。

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

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

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

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

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

guest

回答1

0

自己解決

HTMLの自動フォーマッティングによって、HTMLが崩れていました。
フォーマッティングを無効化して、コーディングしなおすといけました。

投稿2020/09/18 06:26

yosse95ai

総合スコア39

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問