Skip to content

Commit

Permalink
add python unittest (#487)
Browse files Browse the repository at this point in the history
### 背景
> 描述这个 Pull Request 之前代码的状态

### 目标
> 描述这个 Pull Request 要完成的功能

### 其他信息
> 描述这个 Pull Request 相关的其他信息
  • Loading branch information
cairong-ai authored Jan 16, 2024
1 parent 41fa3de commit 86318b9
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
58 changes: 58 additions & 0 deletions .github/workflows/python.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Python

on:
push:
branches: ["main"]
pull_request:
branches: ["main"]

jobs:
build_and_test_python:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.9"

# 以下步骤将在 3kctl 目录下执行
- name: Change to 3kctl directory
run: cd 3kctl

- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
working-directory: 3kctl

- name: Lint with flake8
run: |
pip install flake8
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
working-directory: 3kctl

- name: Test with pytest
run: |
pip install pytest
pytest
working-directory: 3kctl

- name: Test with coverage
run: |
pip install pytest-cov
pytest --cov=./ --cov-report=xml
working-directory: 3kctl

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.xml
fail_ci_if_error: true
verbose: true
working-directory: 3kctl
33 changes: 33 additions & 0 deletions 3kctl/deploy/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import unittest
from unittest.mock import mock_open, patch
from .utils import dict_to_obj, parse_yaml, parse_ini


class TestDictToObj(unittest.TestCase):

def test_dict_to_obj(self):
test_dict = {"a": 1, "b": 2}
obj = dict_to_obj(test_dict)
self.assertEqual(obj.a, 1)
self.assertEqual(obj.b, 2)


class TestParseYaml(unittest.TestCase):

@patch("builtins.open", new_callable=mock_open, read_data="a: 1\nb: 2")
def test_parse_yaml(self, mock_file):
result = parse_yaml()
self.assertEqual(result.a, 1)
self.assertEqual(result.b, 2)


class TestParseIni(unittest.TestCase):

@patch("builtins.open", new_callable=mock_open, read_data="[section]\nkey=value")
def test_parse_ini(self, mock_file):
config = parse_ini()
self.assertEqual(config.section.key, 'value')


if __name__ == '__main__':
unittest.main()

0 comments on commit 86318b9

Please sign in to comment.