-
Notifications
You must be signed in to change notification settings - Fork 256
Refactor tests to use pytest as a test runner for all the packages #732
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
Conversation
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
The tests directory follows the following pattern:
<package>/<test-type>
Where a test type could be an acceptance test (i.e., end-to-end test
on the allure integration itself),
a unit test, a test on a defect, a test on an external library support,
etc. Acceptance tests could be further devided into two groups: tests
to ensure the user API implementation is correct, and tests that check
if allure supports the framework itself properly.
Also contains following changes:
- all: refactor test execution logic into the abstract runner class
- allure_pytest: fix typo in duration_time_test.py
- allure_pytest: fix end enable skipped and xfail tests
- allure_pytest, allure_nose2: remove unnecessary parametrization
from some tests
- allure_pytest: change --log-cli-level to more common --log-level
to test log capturing
- allure_pytest, allure_pytest_bdd: move act phase from fixtures to
tests
- allure_pytest: add less_than assertion to the duration tests
- allure_pytest: the fix second assertion on test_one_fixture_on_two_tests
- allure_pytest: add ids to some parametrized tests
- allure_pytest: fix test file names for them to be collected by pytest
- allure_pytest: remove duplicated parameter set from test_step_parameters
- allure_pytest: remove unnecessary test on missing testplan
- allure_python: remove unicode tests (obsolete since py2 support dropped)
- all: disable pytest automatic plugin loading
- allure_pytest: move testplan and pluginmanager tests to acceptance group
- add test folders to change collection filters - change the linting job to checks all the code base at once - rename the build job to test - remove unnecessary deps installation from lint & test jobs
eroshenkoam
approved these changes
Feb 28, 2023
skhomuti
approved these changes
Mar 1, 2023
beckerGil
pushed a commit
to beckerGil/allure-python
that referenced
this pull request
Apr 10, 2023
2 tasks
IvanBuruyane
pushed a commit
to IvanBuruyane/allure-python
that referenced
this pull request
Mar 19, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
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.
This PR introduces a new testing scheme for the repo. The idea is to use pytest as the main testing framework and express all existing tests in its terms.
Table of contents
Brief usage examples
Package installation
Testing
Collect allure results
Linting
Motivation
Current state
Constraints
Tests diversity and reusability issues
Poor examples
Running tests
Incompatibility between allure-pytest and allure-pytest-bdd
New testing scheme
The
testsfolder structureExamples
Other changes
Linting
Dependency management
Dynamic parameter examples
Allure test result flushing
User experience improvements
Bug fixes
Test fixes and optimizations
Other minor fixes
Documentation fixes
Brief usage examples
All commands are executed from the root of the repo.
Package installation
You may install all packages in editable mode or only one/some of them depending on your needs.
Install all packages
Install all allure-python packages in editable mode and their dependencies (including testing and linting dependencies) with this single command:
Install single package
The sequence remains the same as before:
Example: installing allure_pytest
Testing
Below are some examples on how to run tests.
Run all tests
With poethepoet:
With pytest directly:
Test single package
With poethepoet:
With pytest directly:
Example: test allure-pytest
cd allure-pytest poe testsor
Collect allure results
If you want to run tests with allure enabled, use the
allure-collecttask:Generate and view the report
Use the
allure-generatetask to create an allure report from the previously collected allure results:Use the
allure-opentask to serve the generated report:You can navigate to the report using a browser (if it didn't run automatically).
You can run all tests with allure, generate and open the report using this one-liner:
Linting
Use the
lintertask to run static checks against the entire codebase:Or against a single package:
E.g., for allure-pytest this would be:
cd allure-pytest poe linterMotivation
We have some long-running issues with tests in this repository. To better understand them, lets first describe the current testing scheme.
Current state
Before the change, each framework integration package has had its own end-to-end tests with the following characteristics:
testsfolder (featuresin case of allure-behave).Constraints
While this makes packages self-contained, the cons are quite notable:
Tests diversity and reusability issues
To test an allure integration with a framework you need a test runner to invoke the target framework and some code to assert the results.
Currently, we have integrations with five testing frameworks: behave, nose2, pytest, pytest-bdd and robot framework and, accordingly, behave tests, nose2 tests, pytest tests, pytest-bdd tests and robot framework tests. This results in five different test runner implementations, hence, duplicated and more complex test logic. Also, it's impossible to reuse, say, the code of behave tests in a test that uses robot framework and vice versa.
Additionally,
testsfolders are not included in the package distributions and thus cannot be imported from other modules. Unlike, say, in java, in python we can't conditionally make package subfolders resolvable from other places. The only option here is to move tests to a separate package.There is no strong reason to have this diversity and distribution of tests across packages in the first place. It's much easier from maintainer's perspective to have all tests written using the same framework and contained in a single separate package to share as much code with each other as possible.
Poor examples
Actually, we don't have allure-behave examples at all. What we have in the
allure-behave/featuresfolder is a description on how to test allure-behave. The actual example of usage is just one part of it:Here the example is provided as a step description and the rest of the file is test info on how to check if this example is correct.
Some feature files doesn't include an example on how to use allure with behave at all. They only describe how exactly allure should support the framework and contain no allure API usage inside.
The same applies to allure-pytest-bdd's examples. Examples on
allure-robotframeworklook better but lack of textual descriptions (mostly contain code blocks only).Examples should be written as human readable textual documents with code blocks inside. Test descriptions should go separately.
Running tests
To test all packages, one had to do the following:
allure-python-commonsandallure-python-commons-testpackages.Ideally, this should be doable in two steps:
Incompatibility between allure-pytest and allure-pytest-bdd
There is a known issue (#109) that prevents allure-pytest and allure-pytest-bdd from running simultaneously in the same pytest session. That means, we cannot test either of plugins if we have both of them installed at the same time even if we use the
-ppytest option to disable one of the plugins. That's because the-poption doesn't propagate to a nested pytest session.Another option would be using
PYTEST_DISABLE_PLUGIN_AUTOLOADenvironment variable, but we can't use that because some tests check allure-pytest's support of 3rd party pytest plugins that needs to be loaded.Ideally, this should be fixed in allure-pytest-bdd itself, and I have plans to do that. But for now, there should be a way to explicitly list the pytest plugins required by a test.
New testing scheme
All tests were rewritten using pytest. They were moved from
allure-<framework>folders totests/allure_<framework>. Therefore, thetestsfolder now contains all tests.The
testsfolder structureThe
testsfolder structure now looks like this:The first level
On the first level we have framework-specific test folders, a high-level
conftest.pyand thee2emodule. Potentially, there could be tests on cross-package functionality on that level as well (i.e., compatibility tests).tests/e2e.py
This module contains some functions and classes that come handy for end-to-end tests. Historically, those are almost the only tests we have (e.g., the repo originally contained no unit tests) and there are some common patterns used here allowing us to reuse lots of code. See docstrings on functions and classes themselves to better understand what they could be used for.
tests/conftest.py
This is a high level
conftest.pyof out pytest setup. Its job is to enablepytesterand to declare commonly used fixtures:rst_examplesanddocstring.Framework-specific test folders
A Framework-specific test folder mainly consists of tests on allure integration with the framework. The tests are grouped into nested folders depending on area they test, their granularity, purpose, etc. I used the following categories on the highest level of separation:
acceptance- for end-to-end tests, i.e., tests to ensure the allure fully and correctly supports a corresponding framework. Usually, those tests have doc examples or other form of human readable description associated with them. I further divided acceptance tests into framework support tests (on whether allure correctly translates framework constructs into allure ones) and allure API tests (whether allure API is accessible to a framework user and produces the expected results).defects- for bug reproduction tests.externals- for tests on allure compatibility with 3rd party packages (e.g., pytest plugins).Framework-specific test runners
Each allure integration has its own test runner (except
allure-pytest-bdd; it usesallure-pytest's one) and a fixture associated with it. Each runner inherits from a common base class and implements how exactly the framework should be executed. It also provides a simple way to specify test description (depending on the framework it could be a path to a file, a string or an ID of a code block in an example file or any combination of all these options).Refer to test runner's docstrings on what capabilities does it provides.
Framework-specific conftest.py
These
conftest.pyfiles declare additional fixtures to use by tests. Typically, they are test runner fixtures.Examples
Examples were rewritten as reStructuredTest (.rst) documents with code blocks inside. Each code block has an ID assigned to it. A test may refer to that ID to access the code block content and use it as an input for a framework run.
Here is a fragment of a document, describing how to assign an allure label to a behave test:
The document is human readable. Also, there is a test that checks whether the example is correct:
The test takes the feature file from the example using the code block ID and provides the step definition as an inline string.
Other changes
The PR also contains lots of minor changes.
Linting
Previously, the linting was performed on a per-package basis. It's not necessary, because the linting is fast (compared to building and testing). It performs only static checks, i.e., requires no deps to be installed hence no conflicts.
You can still lint on a per-package basis if you wish, but now there is more general way to lint the entire code base with single
poe lintercommand.The
build allure pythonworkflow was rewritten to use this way of linting instead of creating a full-blown package matrix and linting each package separately. Dependencies installation was removed (linting doesn't require dependencies).Dependency management
Dependency management wasn't the main concern during this work. We definitely have more to do to ease project management but that will be later. The PR contains some improvements though.
Dependencies now could be installed using the following requirement files:
allure_commonsandallure_commons_testin editable mode.See examples above.
Dynamic parameter examples
This was originally in the PR #728. It was moved into this PR to accommodate layout changes.
Closes #727
Obsoletes #728
Allure test result flushing
Allure test result closing was moved from the
pytest_runtest_logfinishhook topytest_runtest_protocol. This makes more sense because this is the last hook in the lifecycle (see the hookspec for more info on that). The only practical implication of this I can think of though is when a test fixture callspytest.exit. Thepytest_runtest_logfinishis not executed in that case, previously leaving the test result unreported.User experience improvements
The PR contains the following changes that affect user experience:
:and=are now allowed between the last period of a tag and its value separator. Previously, only word characters were allowed. This affects robotframework and behave tags.skippedif one of its fixtures callspytest.exit.allure.<link-type>[.<link-name>]:<value>is now supported for link tags in a Robot Framework test case file (see examples).idorselector. Previously, both properties were mandatory.--clear-allurediris specified, all content is now removed from the allure directory. Previously, directories were not deleted. Typically, those were history directories copied from the previous allure report (closes clean-alluredir command line argument removes only files from alluredir root #470).Bug fixes
The following bugs or errors were fixed:
allure_commons.typing.LinkType.TEST_CASEenum value"testcase". It was changed to"tms"as expected by allure reporter (closes Link of type test_case not recognized as proper type in generated report #448).<link type>:[<link text>]<URL>(e.g.,link:[homepage]https://qameta.io) were parsed incorrectly if the link text contains characters from outer parts of the URL due to incorrect usage ofstr.strip.Test fixes and optimizations
The PR contains the following fixes and improvements:
tests/allure_python/acceptance/duration/duration_time_test.py::test_duration[exit](original)tests/allure_python/acceptance/duration/duration_time_test.py::test_with_fixture_duration[exit](original)tests/allure_python/acceptance/duration/duration_time_test.py::test_with_fixture_finalizer_duration[exit](original)tests/allure_python/acceptance/labels/suite/default_suite_test.py::test_default_suite(original)tests/allure_python/acceptance/labels/suite/default_suite_test.py::test_default_class_suite(original)tests/allure_python/acceptance/step/step_placeholder_test.py::test_args_less_than_placeholders(original)tests/allure_python/acceptance/parametrization/parametrization_test.py::test_parametrization_with_ids(original)tests/allure_python/acceptance/parametrization/parametrization_test.py::test_parametrization_decorators_with_partial_ids(original)tests/allure_pytest/acceptance/step/test_step_with_several_step_inside_thread.py::test_step_with_reused_threadstest (original) used thetime.sleepfunction to mix allure steps it creates. This led to a massive time overhead. The steps are now shuffled usingthreading.Event. The test execution time reduced by 30-40 times.--log-cli-levelpytest option was replaced with more common--log-levelin log capture tests.executed_docstring_sourceandexecuted_docstring_pathfixtures were removed. The "act" test phase was moved inside test functions (a fixture should ideally takes care of the "arrange" phase only).tests/allure_pytest/acceptance.fixture.fixture_test.py::test_one_fixture_on_two_tests(originally) test was fixed: the second assertion is now properly executing.step_parameters.pyandcustom_label.pywere renamed tostep_parameters_test.pyandcustom_label_test.pyand now could be collected by pytest as intended.tests/allure_pytest/acceptance/step/step_parameters_test.py::test_step_parameters(originally).Nonetestplan was removed from thetests/allure_pytest/acceptance/testplan/select_test_from_testplan_test.py::test_select_by_testcase_id_test(originally) test as redundant. Almost any other test is aNone-testplan test.tests/allure_pytest/acceptance/testplan/select_test_from_testplan_test.py::test_select_by_testcase_id_test(originally) test was rewritten to not depend upon pytest'sbasetempfolder. Previously, thebasetempfolder had to be nested inside the allure-pytest folder.Other minor fixes
The PR contains the following small changes:
duration_time_test.pywas fixed.allure_pytest.utils.escape_namefunction was removed as it does nothing after parameters were removed from a test full name (closes Allure throws UnicodeDecodeError when recieve \\u in test parameter value #280).select_test_from_testplan_test.pyandpytest_get_allure_plugin_test.pytests were moved to acceptance as they check the pytest support by allure.allure_pytest.utils.allure_titlefunction and in theallure_commons.logger.AllureFileLoggerconstructor.Documentation fixes
The following changes were made in the documentation:
;) to colon (:) in allure-robotframework's README as noted here (closes Documentation error in allure-robot framework. #427).