Skip to content

Add testing with PyTest and GitHub Actions workflow #387

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

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
1ec5de2
Run `ruff` formatter
smokestacklightnin May 3, 2025
5268ae2
Add `pytest` as optional dependency
smokestacklightnin May 3, 2025
de1c9d7
Add `pytest.ini` config file
smokestacklightnin May 3, 2025
211ac53
Add basic test workflow config
smokestacklightnin May 3, 2025
b4201b1
Add checkout step
smokestacklightnin May 3, 2025
a5a7fe7
Add set up Python step
smokestacklightnin May 3, 2025
d2d1e96
Add step to install dependencies
smokestacklightnin May 3, 2025
5000a42
Add step to run tests
smokestacklightnin May 3, 2025
2f193b4
Remove unnecessary `if __name == "__main__"`
smokestacklightnin May 3, 2025
ab1756c
Reformat with `ruff`
smokestacklightnin May 3, 2025
fe1fc5b
Add `xfail` mark to failing test
smokestacklightnin May 3, 2025
d9d8218
Add composite workflow
smokestacklightnin May 3, 2025
523f44e
Add `pytest` as optional dependency
smokestacklightnin May 3, 2025
036a5b8
Run tests for `fairness_indicators` and `tensorboard_plugin` separately
smokestacklightnin May 3, 2025
0445594
Explicitly add shell
smokestacklightnin May 3, 2025
b6246f0
Add more explicit path name
smokestacklightnin May 3, 2025
b23841e
Add `pytest.ini`
smokestacklightnin May 3, 2025
6fb3beb
Fix typo
smokestacklightnin May 3, 2025
b364ec8
Add defaults
smokestacklightnin May 3, 2025
615cbfb
Dont run on pushes with only changes to docs or markdown files
smokestacklightnin May 3, 2025
d7ae2de
Revert formatting changes
smokestacklightnin May 4, 2025
4ea1206
Pin pytest version
smokestacklightnin May 4, 2025
af4382a
Change `xfail` reason
smokestacklightnin May 4, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/actions/setup-env/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Set up environment
description: Set up environment and install package

inputs:
python-version:
default: "3.10"
required: true
package-root-dir:
default: "./"
required: true

runs:
using: composite

steps:
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ inputs.python-version }}
cache-dependency-path: |
${{ inputs.package-root-dir }}/setup.py

- name: Install dependencies
shell: bash
run: |
python -m pip install --upgrade pip
pip install ${{ inputs.package-root-dir }}[test]

38 changes: 38 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Tests
on:
push:
paths-ignore:
- '**.md'
- 'docs/**'
pull_request:
branches: [ master ]
paths-ignore:
- '**.md'
- 'docs/**'
workflow_dispatch:

jobs:
tests:
if: github.actor != 'copybara-service[bot]'
runs-on: ubuntu-latest

strategy:
matrix:
python-version: ['3.9', '3.10']
package-root-dir: ['./', './tensorboard_plugin']

steps:
- name: Checkout repo
uses: actions/checkout@v4

- name: Set up environment
uses: ./.github/actions/setup-env
with:
python-version: ${{ matrix.python-version }}
package-root-dir: ${{ matrix.package-root-dir }}

- name: Run tests
shell: bash
run: |
cd ${{ matrix.package-root-dir }}
pytest
4 changes: 0 additions & 4 deletions fairness_indicators/example_model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,3 @@ def test_example_model(self):
metric_values['fairness_indicators_metrics/[email protected]'],
{'doubleValue': 1.0},
)


if __name__ == '__main__':
tf.test.main()
4 changes: 0 additions & 4 deletions fairness_indicators/remediation/weight_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,3 @@ def test_create_difference_dictionary_bounded_metrics(self):
self.assertIn('gender', res)
self.assertAlmostEqual(res['gender']['female'], 0.125)
self.assertAlmostEqual(res[''][''], 0)


if __name__ == '__main__':
tf.test.main()
3 changes: 0 additions & 3 deletions fairness_indicators/tutorial_utils/util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,3 @@ def test_get_eval_results_called_correclty(self, mock_run_model_analysis,
eval_config=expected_eval_config,
output_path=eval_results_path,
extractors=None)

if __name__ == '__main__':
tf.test.main()
4 changes: 4 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[pytest]
addopts = "--import-mode=importlib"
testpaths = "fairness_indicators"
python_files = "*_test.py"
7 changes: 7 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def select_constraint(default, nightly=None, git_master=None):
'witwidget>=1.4.4,<2',
'protobuf>=3.20.3,<5',
]

TEST_PACKAGES = [
'pytest>=8.3.0,<9',
]
# Get version from version module.
with open('fairness_indicators/version.py') as fp:
globals_dict = {}
Expand All @@ -66,6 +70,9 @@ def select_constraint(default, nightly=None, git_master=None):
python_requires='>=3.9,<4',
install_requires=REQUIRED_PACKAGES,
tests_require=REQUIRED_PACKAGES,
extras_require={
'test': TEST_PACKAGES,
},
# PyPI package information.
classifiers=[
'Development Status :: 4 - Beta',
Expand Down
4 changes: 4 additions & 0 deletions tensorboard_plugin/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[pytest]
addopts = "--import-mode=importlib"
testpaths = "tensorboard_plugin_fairness_indicators"
python_files = "*_test.py"
7 changes: 7 additions & 0 deletions tensorboard_plugin/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def select_constraint(default, nightly=None, git_master=None):
'werkzeug<2',
]

TEST_PACKAGES = [
'pytest>=8.3.0,<9',
]

with open('README.md', 'r', encoding='utf-8') as fh:
long_description = fh.read()

Expand Down Expand Up @@ -80,6 +84,9 @@ def select_constraint(default, nightly=None, git_master=None):
python_requires='>=3.9,<4',
install_requires=REQUIRED_PACKAGES,
tests_require=REQUIRED_PACKAGES,
extras_require={
'test': TEST_PACKAGES,
},
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,3 @@ def testCreateSummaryMetadata_withoutDescription(self):
summary_metadata = metadata.CreateSummaryMetadata()
self.assertEqual(metadata.PLUGIN_NAME,
summary_metadata.plugin_data.plugin_name)


if __name__ == '__main__':
tf.test.main()
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from collections import abc
import os
import pytest
import shutil
from unittest import mock

Expand Down Expand Up @@ -150,6 +151,12 @@ def testIndexJsRoute(self):
response = self._server.get("/data/plugin/fairness_indicators/index.js")
self.assertEqual(200, response.status_code)

@pytest.mark.xfail(
reason=(
"Failing on `master` as of `942b672457e07ac2ac27de0bcc45a4c80276785c`. "
"Please remove once fixed."
)
)
def testVulcanizedTemplateRoute(self):
"""Tests that the /tags route offers the correct run to tag mapping."""
response = self._server.get(
Expand Down Expand Up @@ -222,7 +229,3 @@ def testGetOutputFileFormat(self):
self.assertEqual(
"tfrecord", self._plugin._get_output_file_format("abc_path.tfrecord")
)


if __name__ == "__main__":
tf.test.main()
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,3 @@ def testSummary(self):
six.ensure_text(summary_value.tensor.string_val[0], 'utf-8'))
self.assertEqual(metadata.PLUGIN_NAME,
summary_value.metadata.plugin_data.plugin_name)


if __name__ == '__main__':
tf.test.main()