From cd716e476dd6746d734917f16e72218d0d87005d Mon Sep 17 00:00:00 2001 From: Davintus <91970837+Davintus@users.noreply.github.com> Date: Sat, 17 Jun 2023 14:09:47 +0000 Subject: [PATCH 1/6] Changed test_add() to fail --- .github/workflows/ci_cd_wf.yml | 29 +++++++++++++++++++++++++++++ calculator.py | 2 +- tests/test_calculator.py | 17 +++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/ci_cd_wf.yml diff --git a/.github/workflows/ci_cd_wf.yml b/.github/workflows/ci_cd_wf.yml new file mode 100644 index 00000000..8ca03d4c --- /dev/null +++ b/.github/workflows/ci_cd_wf.yml @@ -0,0 +1,29 @@ +name: test + +on: + push: + branches: + - main + pull_request: + branches: + - "*" + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - name: Check out repo code + uses: actions/checkout@v3 + + - name: Setup Python + uses: actions/setup-python@v3 + with: + python-version: "3.x" + + - name: Install Dependencies + run: | + python -m pip install -r requirements.txt + - name: Run tests + run: | + python -m pytest -v tests \ No newline at end of file diff --git a/calculator.py b/calculator.py index feefb4e3..56954a6d 100644 --- a/calculator.py +++ b/calculator.py @@ -26,4 +26,4 @@ def div(a, b): print("Sum of " + str(a) + " and " + str(b) + " is ", add(a, b)) print("Difference of " + str(a) + " and " + str(b) + " is ", sub(a, b)) print("Product of " + str(a) + " and " + str(b) + " is ", mul(a, b)) - print("Division of " + str(a) + " and " + str(b) + " is ", div(a, b)) + print("Division of " + str(a) + " and " + str(b) + " is ", div(a, b)) \ No newline at end of file diff --git a/tests/test_calculator.py b/tests/test_calculator.py index e69de29b..1a7ee7fb 100644 --- a/tests/test_calculator.py +++ b/tests/test_calculator.py @@ -0,0 +1,17 @@ +from calculator import add, div, mul, sub + + +def test_add(): + assert add(1, 1) == 0 + + +def test_sub(): + assert sub(1, 1) == 0 + + +def test_mul(): + assert mul(1, 1) == 1 + + +def test_div(): + assert div(2, 1) == 2 \ No newline at end of file From 4f3fb5c6017bf30d11eb1f8da750e1e3c2932ce4 Mon Sep 17 00:00:00 2001 From: Davintus <91970837+Davintus@users.noreply.github.com> Date: Sat, 17 Jun 2023 14:22:31 +0000 Subject: [PATCH 2/6] Updated test_add() to pass --- tests/test_calculator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_calculator.py b/tests/test_calculator.py index 1a7ee7fb..aeae4a4d 100644 --- a/tests/test_calculator.py +++ b/tests/test_calculator.py @@ -2,7 +2,7 @@ def test_add(): - assert add(1, 1) == 0 + assert add(1, 1) == 2 def test_sub(): From f4d4699f4ef1f1070630263c309e3fd30b97f3b5 Mon Sep 17 00:00:00 2001 From: Davintus <91970837+Davintus@users.noreply.github.com> Date: Sat, 17 Jun 2023 14:50:10 +0000 Subject: [PATCH 3/6] Added Test Coverage Reports as Comments in Pull Requests --- .github/workflows/ci_cd_wf.yml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci_cd_wf.yml b/.github/workflows/ci_cd_wf.yml index 8ca03d4c..789155d9 100644 --- a/.github/workflows/ci_cd_wf.yml +++ b/.github/workflows/ci_cd_wf.yml @@ -26,4 +26,13 @@ jobs: python -m pip install -r requirements.txt - name: Run tests run: | - python -m pytest -v tests \ No newline at end of file + python -m pytest -v tests + + - name: Run tests and generate coverage report + run: | + python -m pytest -v --cov=my_project tests + + - name: Comment on Pull Request with coverage report + uses: codecov/codecov-action@v2 + with: + token: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file From cfe64775fe871710b467f8e52e94064d08ee5077 Mon Sep 17 00:00:00 2001 From: Davintus <91970837+Davintus@users.noreply.github.com> Date: Sat, 17 Jun 2023 14:57:07 +0000 Subject: [PATCH 4/6] Added Test Coverage Reports as Comments in Pull Requests --- .github/workflows/ci_cd_wf.yml | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci_cd_wf.yml b/.github/workflows/ci_cd_wf.yml index 789155d9..4f09cb24 100644 --- a/.github/workflows/ci_cd_wf.yml +++ b/.github/workflows/ci_cd_wf.yml @@ -30,9 +30,28 @@ jobs: - name: Run tests and generate coverage report run: | - python -m pytest -v --cov=my_project tests + python3 -m pytest --cov=./ --cov-report=xml \ + --verbose \ + --color=yes \ + tests/ - - name: Comment on Pull Request with coverage report - uses: codecov/codecov-action@v2 + # Upload generated XML formatted coverage report artifact which will be later referenced by comment section on PRs + + # Arguments like path, retention_days etc could differ based on your needs. + - name: Upload coverage report to GitHub + uses: actions/upload-artifact@v2.2.4 with: - token: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file + name: coverage-report + path: ./coverage.xml + + # Check if current job is running in the context of a pull request and whether it was triggered by PR event or not. + # If yes, then add comment on that PR providing details about code coverage information. + + - name : Add Code Coverage Report Comments On Pull Request + uses : mshick/add-pr-comment@v1.0.3 + env : + GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }} + with: + message_file_path : ./comment.txt + + \ No newline at end of file From ddb8e0021f0b3f9c33a1ff2f50a31d941da63a60 Mon Sep 17 00:00:00 2001 From: Davintus <91970837+Davintus@users.noreply.github.com> Date: Sat, 17 Jun 2023 14:58:49 +0000 Subject: [PATCH 5/6] Added Test Coverage Reports as Comments in Pull Requests --- .github/workflows/ci_cd_wf.yml | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci_cd_wf.yml b/.github/workflows/ci_cd_wf.yml index 4f09cb24..c41ac426 100644 --- a/.github/workflows/ci_cd_wf.yml +++ b/.github/workflows/ci_cd_wf.yml @@ -47,11 +47,9 @@ jobs: # Check if current job is running in the context of a pull request and whether it was triggered by PR event or not. # If yes, then add comment on that PR providing details about code coverage information. - - name : Add Code Coverage Report Comments On Pull Request - uses : mshick/add-pr-comment@v1.0.3 - env : - GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }} + - name: Comment on Pull Request with coverage report + uses: codecov/codecov-action@v2 with: - message_file_path : ./comment.txt + token: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file From a97f6486337ce67b62ba1c157374e2d6c007ba3c Mon Sep 17 00:00:00 2001 From: Davintus <91970837+Davintus@users.noreply.github.com> Date: Sat, 17 Jun 2023 15:07:54 +0000 Subject: [PATCH 6/6] Added Test Coverage Reports as Comments in Pull Requests --- .github/workflows/ci_cd_wf.yml | 1 + requirements.txt | 2 ++ 2 files changed, 3 insertions(+) diff --git a/.github/workflows/ci_cd_wf.yml b/.github/workflows/ci_cd_wf.yml index c41ac426..d853a685 100644 --- a/.github/workflows/ci_cd_wf.yml +++ b/.github/workflows/ci_cd_wf.yml @@ -24,6 +24,7 @@ jobs: - name: Install Dependencies run: | python -m pip install -r requirements.txt + - name: Run tests run: | python -m pytest -v tests diff --git a/requirements.txt b/requirements.txt index 222e40bb..16de5660 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,5 @@ packaging==23.1 pluggy==1.0.0 pytest==7.3.1 tomli==2.0.1 +pytest-cov==4.1.0 +