Skip to content

Commit 83ec1b4

Browse files
committed
fix: make test setup better
1 parent 66d12dd commit 83ec1b4

File tree

6 files changed

+166
-11
lines changed

6 files changed

+166
-11
lines changed

.coverage

0 Bytes
Binary file not shown.

.github/workflows/pythonpackage.yml

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
name: Json2xml
22

3-
on: [push, pull_request]
3+
on:
4+
push:
5+
branches: [main, master]
6+
paths-ignore:
7+
- 'docs/**'
8+
- '*.md'
9+
- '*.rst'
10+
pull_request:
11+
paths-ignore:
12+
- 'docs/**'
13+
- '*.md'
14+
- '*.rst'
415

516
permissions:
617
contents: read
@@ -25,12 +36,19 @@ jobs:
2536
macos-latest,
2637
]
2738
include:
28-
# Add exact version 3.14.0-alpha.0 for ubuntu-latest only
39+
# Add exact version 3.14.0-beta.1 for ubuntu-latest only
2940
- python-version: 3.14.0-beta.1
3041
tox-python-version: py314-full
3142
os: ubuntu-latest
43+
# Add special linting and type-checking jobs
44+
- python-version: '3.12'
45+
tox-python-version: lint
46+
os: ubuntu-latest
47+
- python-version: '3.12'
48+
tox-python-version: typecheck
49+
os: ubuntu-latest
3250
exclude:
33-
# Exclude other OSes with Python 3.14.0-alpha.0
51+
# Exclude other OSes with Python 3.14.0-beta.1
3452
- python-version: 3.14.0-beta.1
3553
tox-python-version: py314-full
3654
os: windows-latest
@@ -48,25 +66,35 @@ jobs:
4866
with:
4967
python-version: ${{ matrix.python-version }}
5068
allow-prereleases: true
69+
cache: 'pip'
70+
cache-dependency-path: |
71+
requirements*.txt
72+
requirements-dev.in
73+
5174
- name: install uv
5275
uses: astral-sh/setup-uv@v6
5376
with:
5477
enable-cache: true
55-
cache-dependency-glob: requirements-dev.txt
78+
cache-dependency-glob: requirements*.txt
5679

5780
- name: Install dependencies
58-
run: uv pip install --system tox tox-uv
81+
run: |
82+
uv pip install --system tox tox-uv
83+
uv pip install --system pytest pytest-xdist pytest-cov
5984
6085
- name: Run tox targets for ${{ matrix.python-version }}
61-
run: tox -e ${{matrix.tox-python-version}}
86+
run: tox -e ${{matrix.tox-python-version}} --parallel auto
87+
env:
88+
PYTHONPATH: ${{ github.workspace }}
6289

6390
- name: Upload coverage to Codecov
6491
uses: codecov/codecov-action@v5
92+
if: success() && matrix.tox-python-version != 'lint' && matrix.tox-python-version != 'typecheck'
6593
with:
6694
directory: ./coverage/reports/
6795
env_vars: OS,PYTHON
6896
fail_ci_if_error: true
69-
files: ./coverage.xml,./coverage2.xml,!./cache
97+
files: ./coverage.xml
7098
flags: unittests
7199
token: ${{ secrets.CODECOV_TOKEN }}
72100
name: codecov-umbrella

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ test = [
5252

5353
[tool.pytest.ini_options]
5454
testpaths = ["tests"]
55+
python_files = ["test_*.py"]
56+
xvs = true
57+
addopts = "--cov=json2xml --cov-report=xml:coverage/reports/coverage.xml --cov-report=term"
5558
[tool.ruff]
5659
exclude = [
5760
".env",

tests/conftest.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"""Pytest configuration for json2xml tests."""
2+
from __future__ import annotations
3+
4+
import json
5+
import os
6+
from pathlib import Path
7+
from typing import Any, Dict, List, Optional, Union, TYPE_CHECKING
8+
9+
import pytest
10+
11+
if TYPE_CHECKING:
12+
from _pytest.capture import CaptureFixture
13+
from _pytest.fixtures import FixtureRequest
14+
from _pytest.logging import LogCaptureFixture
15+
from _pytest.monkeypatch import MonkeyPatch
16+
from pytest_mock.plugin import MockerFixture
17+
18+
19+
@pytest.fixture
20+
def sample_json_string() -> str:
21+
"""Return a sample JSON string for testing.
22+
23+
Returns:
24+
str: A sample JSON string
25+
"""
26+
return '{"login":"mojombo","id":1,"avatar_url":"https://avatars0.githubusercontent.com/u/1?v=4"}'
27+
28+
29+
@pytest.fixture
30+
def sample_json_dict() -> Dict[str, Any]:
31+
"""Return a sample JSON dictionary for testing.
32+
33+
Returns:
34+
Dict[str, Any]: A sample JSON dictionary
35+
"""
36+
return {
37+
"login": "mojombo",
38+
"id": 1,
39+
"avatar_url": "https://avatars0.githubusercontent.com/u/1?v=4",
40+
}
41+
42+
43+
@pytest.fixture
44+
def sample_json_list() -> List[Dict[str, Any]]:
45+
"""Return a sample JSON list for testing.
46+
47+
Returns:
48+
List[Dict[str, Any]]: A sample list of JSON dictionaries
49+
"""
50+
return [
51+
{
52+
"login": "mojombo",
53+
"id": 1,
54+
"avatar_url": "https://avatars0.githubusercontent.com/u/1?v=4",
55+
},
56+
{
57+
"login": "defunkt",
58+
"id": 2,
59+
"avatar_url": "https://avatars0.githubusercontent.com/u/2?v=4",
60+
},
61+
]
62+
63+
64+
@pytest.fixture
65+
def sample_json_file(tmp_path: Path) -> Path:
66+
"""Create a sample JSON file for testing.
67+
68+
Args:
69+
tmp_path (Path): Pytest temporary path fixture
70+
71+
Returns:
72+
Path: Path to the created JSON file
73+
"""
74+
file_path = tmp_path / "sample.json"
75+
76+
data = {
77+
"login": "mojombo",
78+
"id": 1,
79+
"avatar_url": "https://avatars0.githubusercontent.com/u/1?v=4",
80+
}
81+
82+
with open(file_path, "w") as f:
83+
json.dump(data, f)
84+
85+
return file_path

tests/test_dict2xml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ def test_with_custom_attributes(self):
388388

389389
def test_valid_key(self):
390390
xml = dicttoxml.convert_bool('valid_key', False, False)
391-
assert xml == '<valid_key type="bool">false</valid_key>'
391+
assert xml == '<valid_key>false</valid_key>'
392392

393393
def test_convert_kv_with_cdata(self):
394394
result = dicttoxml.convert_kv("key", "value", attr_type=False, cdata=True)

tox.ini

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,51 @@
11
[tox]
22
envlist = py310, py311, py312, py313, pypy310, pypy311, py314-full
3+
isolated_build = True
4+
skip_missing_interpreters = True
5+
parallel_show_output = True
36

47
[testenv]
8+
passenv =
9+
CI
10+
GITHUB_*
11+
PYTHONPATH
512
deps =
13+
pytest>=8.0.0
14+
pytest-cov>=6.0.0
15+
pytest-xdist>=3.5.0
16+
ruff>=0.3.0
17+
mypy>=1.0.0
18+
types-setuptools
19+
20+
allowlist_externals =
621
pytest
7-
pytest-cov
22+
ruff
23+
mypy
824

9-
allowlist_externals = pytest
25+
commands =
26+
pytest --cov=json2xml --cov-report=xml:coverage/reports/coverage.xml --cov-report=term -xvs {posargs:tests} -n auto
1027

28+
[testenv:lint]
29+
deps =
30+
ruff>=0.3.0
31+
commands =
32+
ruff check json2xml tests
33+
34+
[testenv:typecheck]
35+
deps =
36+
mypy>=1.0.0
37+
types-setuptools
1138
commands =
12-
pytest --cov --cov-report=xml
39+
mypy json2xml tests
40+
41+
[testenv:py314-full]
42+
deps =
43+
{[testenv]deps}
44+
commands =
45+
{[testenv]commands}
46+
ruff check json2xml tests
47+
mypy json2xml tests
48+
49+
[pytest]
50+
testpaths = tests
51+
python_files = test_*.py

0 commit comments

Comments
 (0)