Skip to content

Commit

Permalink
fix: pytestomatio plugin usage with xdist, add tests, sync tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tikolakin committed Dec 8, 2024
1 parent 4c24b6a commit 23820ce
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 7 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ def test_example():
Use python 3.12

1. `pip install ".[dev]"` (note, there are still issues with imports in edit mode `pip install -e ".[dev]"`)
1. `TESTOMATIO_URL=https://beta.testomat.io TESTOMATIO=$TT pytest -p pytester -m smoke`
1. Test things manually (automated test are WIP)
2. `cz commit`
3. `cz bump`
4. `git push remoteName branchName --tags`
1. Verify no regression bugs
1. `cz commit`
1. `cz bump`
1. `git push remoteName branchName --tags`
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ pytestomatio = "pytestomatio.main"
[options.extras_require]
dev = [
"pytest>=7.2.0",
"pytest-testdox>=2.0.0"
"pytest-testdox>=2.0.0",
"pytest-xdist==3.6.1"
]

[tool.pytest.ini_options]
testpaths = ["tests"]
markers = [
"smoke: indicates smoke tests"
]
11 changes: 9 additions & 2 deletions pytestomatio/utils/validations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
from typing import Literal
from pytest import Config
from _pytest.config.exceptions import UsageError



def validate_option(config: Config) -> Literal['sync', 'report', 'remove', 'debug', None]:
Expand All @@ -10,7 +12,12 @@ def validate_option(config: Config) -> Literal['sync', 'report', 'remove', 'debu
if os.getenv('TESTOMATIO') is None:
raise ValueError('TESTOMATIO env variable is not set')

if hasattr(config.option, 'numprocesses') and option in ('sync', 'debug', 'remove'):
raise ValueError('Testomatio does not support parallel sync, remove or report. Remove --numprocesses option')
xdist_plugin = config.pluginmanager.getplugin('xdist')
if xdist_plugin and option in ('sync', 'debug', 'remove'):
if config.option.numprocesses == 0:
return

raise UsageError("The 'sync' mode does not support parallel execution! "
"In order to synchronise test run command sync as '--testomatio sync -n 0'")

return option
4 changes: 4 additions & 0 deletions tests/sub/sub_mob/sub_sub_class_test.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
from pytest import mark
import pytest


class TestClassSubSub:

@pytest.mark.testomatio("@T7e1cf6d3")
def test_one_pass_sub(self):
x = 'this'
assert 'h' in x

@pytest.mark.testomatio("@T64c0abec")
def test_two_fail_sub(self):
x = 'hello'
assert hasattr(x, 'check')

@pytest.mark.testomatio("@Ta488bdcb")
@mark.skip
def test_three_skip_sub(self, dummy_fixture):
x = 'hello'
Expand Down
7 changes: 6 additions & 1 deletion tests/sub/sub_mob/sub_sub_test.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
from pytest import mark
import pytest


@pytest.mark.testomatio("@T761fa328")
def test_pass_sub_sub():
assert 2 + 2 == 4


@pytest.mark.testomatio("@T327cdc55")
def test_pass_fix_sub_sub(dummy_fixture):
assert 3 + 3 == 6


@pytest.mark.testomatio("@T0c63a54a")
def test_fail_sub_sub():
assert 2 + 2 == 11


@pytest.mark.testomatio("@T3dd906d1")
@mark.parametrize('data', [1, 2, 3, 4, 5, 'a'])
def test_ddt_parametrized_sub_sub(data):
assert str(data).isnumeric()


@pytest.mark.testomatio("@T1aec685a")
@mark.skip
def test_skip_sub_sub():
n = 3
Expand Down
3 changes: 3 additions & 0 deletions tests/sub/test_class_sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@

class TestClassSub:

@mark.testomatio("@T7e1cf6d3")
def test_one_pass_sub(self):
x = 'this'
assert 'h' in x

@mark.testomatio("@T64c0abec")
def test_two_fail_sub(self):
x = 'hello'
assert hasattr(x, 'check')

@mark.testomatio("@Ta488bdcb")
@mark.skip
def test_three_skip_sub(self):
x = 'hello'
Expand Down
5 changes: 5 additions & 0 deletions tests/sub/test_sub.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
from pytest import mark


@mark.testomatio("@T9c322c95")
def test_pass_sub():
assert 2 + 2 == 4


@mark.testomatio("@T4e6f250b")
def test_pass_fix_sub(dummy_fixture):
assert 3 + 3 == 6


@mark.testomatio("@T0bf7108d")
def test_fail_sub():
assert 2 + 2 == 11


@mark.testomatio("@T7e069711")
@mark.parametrize('data', [1, 2, 3, 4, 5])
def test_ddt_parametrized_sub(data):
assert str(data).isnumeric()


@mark.testomatio("@Tad0d98ed")
@mark.skip
def test_skip_sub():
n = 3
Expand Down
6 changes: 6 additions & 0 deletions tests/test_cli_param_test_id.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pytest
pytestmark = pytest.mark.smoke

test_file = """
import pytest
Expand All @@ -17,6 +19,7 @@ def test_neither_marker():
pass
"""

@pytest.mark.testomatio("@T7b058966")
def test_cli_param_test_id_without_filters(pytester):
pytester.makepyfile(test_file)

Expand All @@ -29,6 +32,7 @@ def test_cli_param_test_id_without_filters(pytester):
"*::test_neither_marker PASSED*",
])

@pytest.mark.testomatio("@T3cf626ca")
def test_cli_param_test_id_with_k_filter(pytester):
pytester.makepyfile(test_file)

Expand All @@ -38,6 +42,7 @@ def test_cli_param_test_id_with_k_filter(pytester):
"*::test_neither_marker PASSED*",
])

@pytest.mark.testomatio("@T709adc8a")
def test_cli_param_test_id_without_k_filter_matching_2_tests(pytester):
pytester.makepyfile(test_file)

Expand All @@ -51,6 +56,7 @@ def test_cli_param_test_id_without_k_filter_matching_2_tests(pytester):
# TODO: troubleshoot pytester env
# The testomatio and test-id parameters are lost in the pytester env.
# Please test it in a semiautomated way with "test_cli_params.py" test
@pytest.mark.testomatio("@T5a965adf")
@pytest.mark.skip
def test_cli_param_test_id_with_test_id_filter(pytester):
pytester.makepyfile(test_file)
Expand Down
3 changes: 3 additions & 0 deletions tests/test_cli_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
# ======================================= 3 passed, 50 deselected in 0.89s =======================================

import pytest
pytestmark = pytest.mark.smoke

@pytest.mark.testomatio("@T55ecbca9")
def test_smoke():
pass

Expand All @@ -19,5 +21,6 @@ def test_testomatio_only():
def test_smoke_and_testomatio():
pass

@pytest.mark.testomatio("@T06f3da52")
def test_neither_marker():
pass
2 changes: 2 additions & 0 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ def test_something():
assert 1 == 1


@mark.testomatio("@T81850b4b")
def test_no_decorator():
assert 1 == 1


@mark.testomatio("@T9c91e8e7")
def test_some_test():
x = os.getenv('TESTOMATIO_CODE_STYLE')
assert x == 'pep8'
46 changes: 46 additions & 0 deletions tests/test_xdist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import pytest

@pytest.mark.testomatio("@Tfaf4da53")
@pytest.mark.smoke
def test_sync_stop_when_xdist_in_use(pytester):
pytester.makepyfile("""
def test_example():
assert True
""")

# Ensure that your plugin code raises UsageError for this scenario instead of a generic Exception.
# Something like:
# if option == 'sync' and parallel_set:
# raise UsageError("The 'sync' mode does not support parallel execution! In order to synchronise test run command sync as '--testomatio sync -n 0'")

result = pytester.runpytest_inprocess('-p', 'xdist', '--testomatio', 'sync', '-vv')

# Match the entire error line as it appears in stderr
result.stderr.fnmatch_lines([
"ERROR: The 'sync' mode does not support parallel execution! In order to synchronise test run command sync as '--testomatio sync -n 0'"
])

# Now that it's a usage error, pytest should produce a summary line that we can assert on
assert result.ret != 0

@pytest.mark.smoke
def test_sync_works_with_xdist_set_to_0(pytester):
pytester.makepyfile("""
def test_example():
assert True
""")

result = pytester.runpytest_inprocess('-p', 'xdist', '--testomatio', 'sync', '-n', '0', '-vv')

# Assert that the special exit message is printed to stderr
result.stdout.fnmatch_lines([
"*Sync completed without test execution*"
])

# Assert that no tests were run
result.stdout.fnmatch_lines([
"*no tests ran in *"
])

# Optional: Verify the process exited successfully (0 means no error)
assert result.ret == 2

0 comments on commit 23820ce

Please sign in to comment.