-
Notifications
You must be signed in to change notification settings - Fork 1k
Pytest organization proposal #1605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| # Pytest organization proposal | ||
|
|
||
| This proposal intends to achieve the following | ||
| - Have a standard way of categorizing tests | ||
| - Have some guidelines around test file organization | ||
| - Have a master wrapper for test execution | ||
| - Follow common documentation style | ||
| - Test result collection | ||
|
|
||
| ## Test categorization | ||
| Leverage pytest custom markers to group tests based on topology, asic, features, device type and connection type. | ||
| Every testcase needs to have a topology marker. Feature markers are recommended for any feature test that are getting added. | ||
| 'Device_type' is optional but needs to be specified if there is a specific requirement that the test needs a physical DUT as opposed to a VS. The same criteria applies for 'connection_type' | ||
|
|
||
| ``` | ||
| pytest.ini | ||
| [pytest] | ||
| markers: | ||
| topology(topo_name): The topologies this particular testcase can run against. topo_name can be individual topology names like 't0', 't1', 'ptf', 'any' or a comma separated like ('t0', 't1') if supported on multiple topologies | ||
| asic(vendor_name): used for asic specific test(broadcom, mellanox etc) | ||
| feature(feature_name): feature this test is written for. eg. acl, nat | ||
| connection_type(name): names can be 'fabric' (which indicates the presence of a fanout switch) or 'direct' if a testcase uses directly connected links | ||
| device_type(name): name can 'physical' (if this test requires a physical dut) or 'vs' (if this test can be run on a virtual switch) | ||
|
|
||
| ``` | ||
| conftest.py | ||
|
|
||
| ``` | ||
| def pytest_addoption(parser): | ||
| parser.addoption("--topology", action="store", metavar="NAME", | ||
| help="only run tests matching the topology NAME") | ||
|
|
||
| def pytest_runtest_setup(item): | ||
| toponames = [mark.args for mark in item.iter_markers(name="topology")] | ||
| if toponames: | ||
| cfg_topos = item.config.getoption("--topology").split(',') | ||
| if all(topo not in toponames[0] for topo in cfg_topos): | ||
| pytest.skip("test requires topology in {!r}".format(toponames)) | ||
| else: | ||
| if item.config.getoption("--topology"): | ||
| pytest.skip("test does not match topology") | ||
|
|
||
| ``` | ||
|
|
||
| Sample test file: test_topo.py | ||
|
|
||
| ``` | ||
| @pytest.mark.topology('t0', 't1') | ||
| def test_all(): | ||
| assert 1 == 1 | ||
|
|
||
| @pytest.mark.topology('t0') | ||
| def test_t0(): | ||
| assert 1 == 1 | ||
|
|
||
|
|
||
| @pytest.mark.topology('any') | ||
| def test_any(): | ||
| assert 1 == 1 | ||
|
|
||
| ``` | ||
|
|
||
| Sample test file: test_notopo.py | ||
|
|
||
| ``` | ||
| def test_notopo(): | ||
| assert 1 == 1 | ||
|
|
||
| ``` | ||
|
|
||
| Test run | ||
|
|
||
| ``` | ||
| py.test --inventory inv --host-pattern dut1 --module-path ../ansible/library/ --testbed tb --testbed_file tb.csv --topology t1 test_topo.py test_notopo.py -rA | ||
|
|
||
| platform linux2 -- Python 2.7.12, pytest-4.6.9, py-1.8.1, pluggy-0.13.1 | ||
| ansible: 2.8.7 | ||
| rootdir: /var/nejo/Networking-acs-sonic-mgmt/tests, inifile: pytest.ini | ||
| plugins: ansible-2.2.2 | ||
| collected 4 items | ||
|
|
||
| test_topo.py::test_all PASSED [ 25%] | ||
| test_topo.py::test_t0 PASSED [ 50%] | ||
| test_topo.py::test_any SKIPPED [ 75%] | ||
| test_notopo.py::test_notopo SKIPPED [100%] | ||
|
|
||
| .... | ||
|
|
||
| .... | ||
| PASSED test_topo.py::test_all | ||
| SKIPPED [1] /var/nejo/Networking-acs-sonic-mgmt/tests/conftest.py:293: test requires topology in [('t0',)] | ||
| SKIPPED [1] /var/nejo/Networking-acs-sonic-mgmt/tests/conftest.py:293: test requires topology in [('any',)] | ||
| SKIPPED [1] /var/nejo/Networking-acs-sonic-mgmt/tests/conftest.py:295: test does not match topology | ||
|
|
||
| ``` | ||
|
|
||
| ## Test file organization | ||
| - Have 2 broad categories (platform and feature). Feature specific tests and their helpers go into specific feature folders. | ||
|
|
||
| ``` | ||
| tests | ||
| |_ common | ||
| |_ platform | ||
| |_ ptftests | ||
| |_ nat | ||
| |_ test_nat_bindings.py | ||
| |_ files | ||
| |_ all helpers for the nat feature | ||
| |_ acl | ||
|
|
||
| ``` | ||
|
|
||
| - Any reusable code needs to go under tests/common | ||
|
|
||
| - File naming convention | ||
| The objective here is to provide meaningful names for helper files/testcase files so that the user gets a general idea of the file contents. | ||
|
|
||
|
|
||
| ## Master wrapper | ||
| Make it easier to run a nightly test against a feature/platform/topology from the command line. Have something similar to the 'ansible/testbed-cli.sh' script which can be invoked with just the basic parameters (testbed name, what flavor of test to run) | ||
|
|
||
|
|
||
| ## Documentation style | ||
| Follow a common style of documentation for test methods which can be used by some tool to generate html content | ||
|
|
||
|
|
||
| ## Test result collection | ||
| Use the --junitxml attribute to collect test results. Can leverage the existing format used in sonic-utilities/sonic-swss repo for reporting test results. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| # Pytest organization proposal | ||
|
|
||
| This proposal intends to achieve the following | ||
| - Have a standard way of categorizing tests | ||
| - Have some guidelines around test file organization | ||
| - Have a master wrapper for test execution | ||
| - Follow common documentation style | ||
| - Test result collection | ||
|
|
||
| ## Test categorization | ||
| Leverage pytest custom markers to group tests based on topology, asic, features, device type and connection type. | ||
| Every testcase needs to have a topology marker. Feature markers are recommended for any feature test that are getting added. | ||
| 'Device_type' is optional but needs to be specified if there is a specific requirement that the test needs a physical DUT as opposed to a VS. The same criteria applies for 'connection_type' | ||
|
|
||
| ``` | ||
| pytest.ini | ||
| [pytest] | ||
| markers: | ||
| topology(topo_name): The topologies this particular testcase can run against. topo_name can be individual topology names like 't0', 't1', 'ptf', 'any' or a comma separated like ('t0', 't1') if supported on multiple topologies | ||
| asic(vendor_name): used for asic specific test(broadcom, mellanox etc) | ||
| feature(feature_name): feature this test is written for. eg. acl, nat | ||
| connection_type(name): names can be 'fabric' (which indicates the presence of a fanout switch) or 'direct' if a testcase uses directly connected links | ||
| device_type(name): name can 'physical' (if this test requires a physical dut) or 'vs' (if this test can be run on a virtual switch) | ||
|
|
||
| ``` | ||
| conftest.py | ||
|
|
||
| ``` | ||
| def pytest_addoption(parser): | ||
| parser.addoption("--topology", action="store", metavar="NAME", | ||
| help="only run tests matching the topology NAME") | ||
|
|
||
| def pytest_runtest_setup(item): | ||
| toponames = [mark.args for mark in item.iter_markers(name="topology")] | ||
| if toponames: | ||
| cfg_topos = item.config.getoption("--topology").split(',') | ||
| if all(topo not in toponames[0] for topo in cfg_topos): | ||
| pytest.skip("test requires topology in {!r}".format(toponames)) | ||
daall marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| else: | ||
| if item.config.getoption("--topology"): | ||
| pytest.skip("test does not match topology") | ||
|
|
||
neethajohn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| Sample test file: test_topo.py | ||
|
|
||
| ``` | ||
| @pytest.mark.topology('t0', 't1') | ||
| def test_all(): | ||
| assert 1 == 1 | ||
|
|
||
| @pytest.mark.topology('t0') | ||
| def test_t0(): | ||
| assert 1 == 1 | ||
|
|
||
|
|
||
| @pytest.mark.topology('any') | ||
| def test_any(): | ||
| assert 1 == 1 | ||
|
|
||
| ``` | ||
|
|
||
| Sample test file: test_notopo.py | ||
|
|
||
| ``` | ||
| def test_notopo(): | ||
| assert 1 == 1 | ||
|
|
||
| ``` | ||
|
|
||
| Test run | ||
|
|
||
| ``` | ||
| py.test --inventory inv --host-pattern dut1 --module-path ../ansible/library/ --testbed tb --testbed_file tb.csv --topology t1 test_topo.py test_notopo.py -rA | ||
neethajohn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| platform linux2 -- Python 2.7.12, pytest-4.6.9, py-1.8.1, pluggy-0.13.1 | ||
| ansible: 2.8.7 | ||
| rootdir: /var/nejo/Networking-acs-sonic-mgmt/tests, inifile: pytest.ini | ||
| plugins: ansible-2.2.2 | ||
| collected 4 items | ||
|
|
||
| test_topo.py::test_all PASSED [ 25%] | ||
| test_topo.py::test_t0 PASSED [ 50%] | ||
| test_topo.py::test_any SKIPPED [ 75%] | ||
| test_notopo.py::test_notopo SKIPPED [100%] | ||
|
|
||
| .... | ||
|
|
||
| .... | ||
| PASSED test_topo.py::test_all | ||
| SKIPPED [1] /var/nejo/Networking-acs-sonic-mgmt/tests/conftest.py:293: test requires topology in [('t0',)] | ||
| SKIPPED [1] /var/nejo/Networking-acs-sonic-mgmt/tests/conftest.py:293: test requires topology in [('any',)] | ||
| SKIPPED [1] /var/nejo/Networking-acs-sonic-mgmt/tests/conftest.py:295: test does not match topology | ||
|
|
||
| ``` | ||
|
|
||
| ## Test file organization | ||
| - Have 2 broad categories (platform and feature). Feature specific tests and their helpers go into specific feature folders. | ||
|
|
||
| ``` | ||
| tests | ||
| |_ common | ||
| |_ platform | ||
daall marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| |_ ptftests | ||
| |_ nat | ||
| |_ test_nat_bindings.py | ||
| |_ files | ||
| |_ all helpers for the nat feature | ||
| |_ acl | ||
|
|
||
neethajohn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| - Any reusable code needs to go under tests/common | ||
|
|
||
| - File naming convention | ||
| The objective here is to provide meaningful names for helper files/testcase files so that the user gets a general idea of the file contents. | ||
|
|
||
|
|
||
| ## Master wrapper | ||
| Make it easier to run a nightly test against a feature/platform/topology from the command line. Have something similar to the 'ansible/testbed-cli.sh' script which can be invoked with just the basic parameters (testbed name, what flavor of test to run) | ||
|
|
||
|
|
||
| ## Documentation style | ||
| Follow a common style of documentation for test methods which can be used by some tool to generate html content | ||
|
|
||
|
|
||
| ## Test result collection | ||
| Use the --junitxml attribute to collect test results. Can leverage the existing format used in sonic-utilities/sonic-swss repo for reporting test results. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.