• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer

AI活用エンジニアへの道

生成AI時代のエンジニアの生き方を考えるメディア(作成途中)

  • コラム
  • 生成AI
  • プログラミング
    • Python入門
    • Flask入門
    • Django入門
  • プロフィール
Home / プログラミング / Python / PythonにてFakerを用いてランダムなテストデータを生成する方法

PythonにてFakerを用いてランダムなテストデータを生成する方法

1 PythonにてFakerを用いてテストデータを生成する方法
2 関連記事

PythonにてFakerを用いてテストデータを生成する方法

結合試験、総合試験などの際に、DBにテストデータを作成することがあるが、pythonではFakerを利用すると便利。

Fakerを利用することで、一つ一つ自分で考えずにランダムで名前や電話番号を自動で生成することができる。
ここでは、Fakerのインストールから利用方法を紹介する

  • Fakerのインストール

pip install faker

 

  • Fakerの使い方(例の通り、メソッドを呼び出すとランダムで値が取れる
from faker import Faker # fakerのインポート

fakegen = Faker('ja_JP') # Fakerのインスタンス作成、引数をja_JPとすると日本向けのテストデータ作成用のインスタンス

fakegen.name() # 田辺 花子(ランダムな名前)

fakegen.name() # 松本 美加子

fakegen.company() # '合同会社笹田情報'

上の例では
name,companyを利用したが、
の他にもいろいろなランダム値を取れる。

代表的なものだと

  • address・・・住所
  • name・・・名前
  • city・・・都市名
  • company・・・会社名
  • date・・・日付
  • email・・・メールアドレス
  • text・・・てきとうな文
  • zipcode・・・郵便番号

などがある。

以下にFakerのその他のメソッドの一覧を記述する

  • Fakerのメソッド一覧
add_provider address am_pm ascii_company_email
ascii_email ascii_free_email ascii_safe_email ban
bank_country bban binary boolean
bothify bs building_name building_number
catch_phrase century chome chrome
city city_suffix color_name company
company_category company_email company_prefix company_suffix
coordinate country country_code credit_card_expire
credit_card_full credit_card_number credit_card_provider credit_card_security_code
cryptocurrency cryptocurrency_code cryptocurrency_name currency
currency_code currency_name date date_between
date_between_dates date_object date_of_birth date_this_century
date_this_decade date_this_month date_this_year date_time
date_time_ad date_time_between date_time_between_dates date_time_this_century
date_time_this_decade date_time_this_month date_time_this_year day_of_month
day_of_week domain_name domain_word ean
ean13 ean8 ein email
file_extension file_name file_path firefox
first_kana_name first_kana_name_female first_kana_name_male first_name
first_name_female first_name_male first_romanized_name first_romanized_name_female
first_romanized_name_male format free_email free_email_domain
future_date future_datetime get_formatter get_providers
gou hex_color hexify hostname
iban image_url internet_explorer ipv4
ipv4_network_class ipv4_private ipv4_public ipv6
isbn10 isbn13 iso8601 itin
job kana_name language_code last_kana_name
last_name last_name_female last_name_male last_romanized_name
latitude latlng lexify license_plate
linux_platform_token linux_processor local_latlng locale
location_on_land longitude mac_address mac_platform_token
mac_processor md5 mime_type month
month_name msisdn name name_female
name_male null_boolean numerify opera
paragraph paragraphs parse password
past_date past_datetime phone_number postcode
prefecture prefix prefix_female prefix_male
profile provider providers pybool
pydecimal pydict pyfloat pyint
pyiterable pylist pyset pystr
pystruct pytuple random random_choices
random_digit random_digit_not_null random_digit_not_null_or_empty random_digit_or_empty
random_element random_elements random_int random_letter
random_letters random_lowercase_letter random_number random_sample
random_uppercase_letter randomize_nb_elements rgb_color rgb_css_color
romanized_name safari safe_color_name safe_email
safe_hex_color seed seed_instance sentence
sentences set_formatter sha1 sha256
simple_profile slug ssn street_address
street_name street_suffix suffix suffix_female
suffix_male text time time_delta
time_object time_series timezone tld
town unix_device unix_partition unix_time
uri uri_extension uri_page uri_path
url user_agent user_name uuid4
windows_platform_token word words year
zipcode

関連記事

Pythonの型ヒント:コレクション型とOptional型の使い方
Pythonの型ヒント:コレクション型とOptional型の…
Python型ヒントについて、変数と関数の引数と戻り値に付与する基本的な書き方について
Python型ヒントについて、変数と関数の引数と戻り値に付与…
【まとめ】よく使うLinuxのショートカット11選
【まとめ】よく使うLinuxのショートカット11選
覚えておくべきLinuxコマンド50選
覚えておくべきLinuxコマンド50選
FlaskでFormを用いてPOSTリクエストを行い、送信された値を取得する
FlaskでFormを用いてPOSTリクエストを行い、送信さ…
Flaskでabortを用いてHTTPException例外を強制的に発生させる
Flaskでabortを用いてHTTPException例外…

Primary Sidebar

人気記事

  • 1
    ITエンジニアとしてスキルを高めるためのUdemy講…
  • 2
    PyCharm,IntelliJのおすすめプラグイン一覧
  • no image 3
    Python未経験だが、フリーランスの案件に採用さ…
  • 4
    アメリカのシリコンバレーでプログラミングを無…

特集記事

  • 1
    Cursorの使用のポイントとコツ(コード補完、イ…
  • 2
    ITエンジニアとしてスキルを高めるためのUdemy講…

目次

1 PythonにてFakerを用いてテストデータを生成する方法
2 関連記事
StartCode ロゴ
AI時代のエンジニアを最速で育てる
法人向けIT研修で即戦力を育成しています。詳しくはお問い合わせください。
▶ 詳細はこちら
Python講座
【Udemy】Python入門講座(0からPythonの基礎を体系的に学んで、生成AIでバイブコーディング)
受講生3000人ほどの人気講座。<strong>Pythonの基礎から生成AI活用</strong>...
詳細 ▶

Footer

利用規約