Data driven test framework
Project description
PyDataTest
Data driven python test framework
Document
Introduction
Aim to provide a simple way to write testcase, especially when test same logic with different data input and output. So PyDataTest wrap unittest with a set of utilities to test with data.
Install
pip install pydatatest # with pip
poetry add pydatatest # or with poetry
pipenv add pydatatest # with pipenv
Usage
- Get a runner instance
runner = pydatatest.runner()
- Write a test case and inject data including input and output
data = [["abc","123"], ['aaa',"000000"]] @inject_def(['passport', 'password'], session=True) @test_with(runner) # Register this testcase, also can use runner name, eg: @test_with("runner1") class TestUserLogin(PyDataTestCase): def setUp(self): print("login test start\n") @inject(["abc", "123456"]) def test_01(self): self.assertEqual(self.passport, 'hitest') @inject(data, True) def test_02(self): self.assertEqual(self.password, '111111') def tearDown(self): print("login test end\n")
- Run with PyDataTest
runner.run()
API
runner
A framework instance that will run all testcases Example:
r = pydatatest.runner("my runner") r.add_test(testcase) # add a test case manually r.run() # start test
PyDataTestCase
Data test Instance Attributes: session: request session, can be used in many test method to share some state injected variables: You can access it in your test method as self.variable_name(the name is as defined in your @inject_def)
Methods: before_all: run before all cases after_all: run after all cases before_each: run before all run of every test method after_each: run before all run of every test method before_each_data: run before every run of every test method after_each_data: run after every run of every test method test**: test case method, each method will be run several times accoding to the injected data You can also use the unittest api here, such as self.assertEqual, for more information, see Assert methods and unittest
Example:
@inject_def(['passport', 'password'], session=True) @test_with(myrunner) class TestUserLogin(PyDataTestCase): @classmethod def before_all(cls): print("before all test") @classmethod def after_all(cls): print("after all test") def before_each(self): print("before_each test") def after_each(self): print("after_each test") def before_each_data(self): print("before_each_data test") def after_each_data(self): print("after_each_data test") @inject(data[0]) def test_01(self): self.assertEqual(self.passport, 'username') @inject(data, multi=True) def test_02(self): print(self.passport) print(self.password) self.assertEqual(self.password, 'password') @inject(["username", "password1"]) def test_03(self): self.assertEqual(self.password, 'password')
@test_with
Register a testcase Params: runner: string or runner instance, you can use runner name to avoid loop dependency
@inject_def and @inject
Example:
@inject_def(['passport', 'password'], session=True) class TestCaseClass(PyDataTestCase): @inject(["passport1", "password1"]) def test_01(self): print(self.passport) # defined variable can be access here
@inject_def
Define inject data varibales Params:
variables: injected data will be parsed according to the sequence of variablessession: if True means that this case will use request session
@inject
Inject data to a test method Params:
data: list like data, will be parsed according to its sequence (same as@inject_def)multi: if true, the data will be seen as list of many group of variable, and this test method will be run the same times as the length of the data. Each group of variable will be run once.
TODO
- @depends_on: to run multicase pipeline automatically
- @inject_yaml: to inject yaml data
- @inject_csv: to inject csv data
- @once: indicates that the test case should be run once
- @expected: indicates that the test case should return expected output
- @throws: indicates that the test case should throw specific exception
- More corner case
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
File details
Details for the file pydatatest-1.0.1.tar.gz.
File metadata
- Download URL: pydatatest-1.0.1.tar.gz
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bfbdf1a8412029962ce55c8ce9571e92b1a3d911f921276bb0ce582e9e5e6dd7
|
|
| MD5 |
4b718a6e7efd09f59d0ff9fdb98412ca
|
|
| BLAKE2b-256 |
a36ed58c9304e596906a2e00471b81a2f553d87a4a78f8a75b40e2cfef54c887
|