Data Driven Testing¶
Data Driven Testing allows to dynamically generate tests using a data source. The data sources can be Python objects or data files.
Tests are turned into data driven tests by using decorators.
You can find more information in the Data Driven Testing documentation.
Pinging a List of Hosts¶
You can easily create a test prototype that can ping a host that is supplied as a parameter, and then turn this test into multiple tests trying to ping multiple hosts.
The ping demo is located in data_driven_testing/test_ping_hosts.py.
#
# Copyright (c) 2023, HILSTER - https://hilster.io
# All rights reserved.
#
import htf
import os
import platform
"""
This test case shows how to ping many hosts using data driven testing.
"""
def ping(hostname: str) -> None:
if platform.platform().startswith("Linux"):
response = os.system("ping -c 1 " + hostname)
else:
response = os.system("ping -n 1 " + hostname)
htf.assert_equal(response, 0, "the host {} could not be pinged".format(hostname))
@htf.test
def ping_single_host() -> None:
ping("localhost")
@htf.data(["8.8.8.8", "heise.de", "hilster.io"])
def test_ping_hosts(hostname: str) -> None:
ping(hostname)
@htf.yaml_data("hosts.yml")
def test_ping_hosts_file(hostname: str) -> None:
ping(hostname)
if __name__ == "__main__":
htf.main()
To run the demo, run
cd data_driven_testing
htf -o test_ping_hosts.py
To run the demo with asyncio, run
cd data_driven_testing_async
htf -o test_ping_hosts.py
Data-driven host tests
Download recording · Download transcript
Data-driven host tests
======================
Visual transcript and walkthrough of the silent recording.
Based on reviewed screen captions/interface actions and the associated written example.
Original recording: demo_ddt_pinging.mp4
1. Change to the data_driven_testing directory and execute test_ping_hosts.py.
2. The terminal runs the example for the supplied hosts and reports their results.
3. The generated HTML report lists the individual data-driven test instances, including
their host parameters.
4. The chapter contains the current host-test source and its execution command.
External Test Data¶
It is also possible to put test data into external files that could be generated elsewhere.
The demo includes examples for YAML- and CSV-data.
This test data demo is located in data_driven_testing/test_data.py.
#
# Copyright (c) 2023, HILSTER - https://hilster.io
# All rights reserved.
#
import htf
"""
This test case shows how to run data driven tests with external test data.
"""
@htf.yaml_data("test_data.yml")
def test_data_yaml(hostname: str, port: int) -> None:
"""
The dictionaries in the YAML data source must match the parameters.
A test is dynamically generatged for each item in the test data list.
"""
print("test", hostname, port)
@htf.csv_data("test_data.csv")
def test_data_csv(hostname: str, port: int) -> None:
"""
The headers must match the parameters.
A test is dynamically generated for each line in the csv data.
"""
print("test", hostname, port)
@htf.data(range(100))
def test_success(number: int) -> None:
pass
if __name__ == "__main__":
htf.main()
To run the demo, run
cd data_driven_testing
htf -o test_data.py
To run the demo with asyncio, run
cd data_driven_testing_async
htf -o test_data.py
Tests supplied by external data
Download recording · Download transcript
Tests supplied by external data
===============================
Visual transcript and walkthrough of the silent recording.
Based on reviewed screen captions/interface actions and the associated written example.
Original recording: demo_ddt_externaldata.mp4
1. Change to the data_driven_testing directory and execute test_data.py.
2. The recording shows external data files alongside the terminal and the generated HTML
report.
3. Test instances are generated from the supplied data. Opening an instance in the
report displays its parameters, such as a host and port.
4. The source listing and input files in the chapter explain how the current example
consumes external data.
Combinatorial Tests¶
Let’s assume you have a list of hosts and a list of ports of services you’d like to test.
You can dynamically create tests with multiple data files using the combinatorial decorators.
The demo is located in data_driven_testing/test_combinatorial.py.
#
# Copyright (c) 2023, HILSTER - https://hilster.io
# All rights reserved.
#
import htf
@htf.product(htf.YAMLFileIterator("hosts.yml"), htf.YAMLFileIterator("ports.yml"))
def test_hostname_and_service(hostname: str, port: int) -> None:
print("test", hostname, port)
if __name__ == "__main__":
htf.main()
To run the demo, run
cd data_driven_testing
htf -o test_combinatorial.py
To run the demo with asyncio, run
cd data_driven_testing_async
htf -o test_combinatorial.py
Combinatorial test data
Download recording · Download transcript
Combinatorial test data
=======================
Visual transcript and walkthrough of the silent recording.
Based on reviewed screen captions/interface actions and the associated written example.
Original recording: demo_ddt_combinatoric.mp4
1. Change to the data_driven_testing directory and run the combinatorial example, named
test_combinatoric.py in the historical recording.
2. htf combines values from hosts.yml and ports.yml to generate host-and-port test
cases.
3. The terminal reports the generated cases; the HTML report displays their host and
port parameters.
4. The on-screen explanation lists YAML, JSON, and CSV as supported external-data
formats. The current chapter names its source example test_combinatorial.py.