Skip to main content

Get and load testdata in pytest projects

Project description

pytest-testdata

Languate - Python PyPI - License PyPI PyPI - Downloads

Pytest测试项目数据目录配置及数据文件加载


如何使用

  1. 安装 pytest-testdata

使用pip安装

pip install pytest-testdata
  1. 配置测试目录

默认测试目录为 项目跟目录 (rootdir) / data

也可以通过命令行参数或pytest.ini配置修改

pytest . --testdata-dr=testdata

或在pytest.ini中配置

[pytest]
testdata_dir=testdata
  1. 测试中使用测试目录或测试数据
  • Fixture函数testdata_dir: 当前项目测试数据目录路径
  • Fixture函数testdata: 测试数据加载对象

例如,测试项目中数据如下

project
  data/
    1.txt
    2.png
    3.json
import json

def test_use_testdata_01(testdata_dir):
    data1 = (testdata_dir / '1.txt').read()
    data2 = (testdata_dir / '2.png').read_bytes()
    with open(testdata_dir / '3.json') as f:
        data3 = json.load(f)
    
def test_use_testdata_02(testdata):
    data1 = testdata.read('1.txt')
    data2 = testdata.read_bytes('2.png')
    data3 = testdata.load('3.json')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_testdata-0.1.0.tar.gz (3.7 kB view details)

Uploaded Source

File details

Details for the file pytest_testdata-0.1.0.tar.gz.

File metadata

  • Download URL: pytest_testdata-0.1.0.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.6

File hashes

Hashes for pytest_testdata-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f8c39af7efc9499b2363dbadbd7c651d0dd8dbb89c1b94939550585a9fd9de31
MD5 e7f517740dd704b25cc94f98974e9c7e
BLAKE2b-256 c321a996f8980ec573ccc0dc4b408ffc68195cd33d849e81c300f744577d93ef

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page