'write test cases for python code in Bitbucket pipeline
I'm trying to set up Integrated CI/CD for Bitbucket Cloud that helps automate code from test to production in the Cloud and want to run some test cases in the bitbucket pipeline for simple python code of iseven() function, want to implement it like that if any one of these test cases fails then it should not be pushed means I'll intentionally make that function will return True both time and then it should not to be push on the remote repo due to the pipeline Test case is failed. hope you'll get what I am trying to say.
import unittest
def iseven(num):
# checking that if number is even return True
if num % 2 == 0:
return True
# checking that if number is not even return False
elif num % 2 !=0:
return False
class TestIsEvenMethod(unittest.TestCase):
def test_iseven1(self):
self.assertEqual(iseven(12),True)
def test_iseven2(self):
self.assertEqual(iseven(11),False)
if __name__ == '__main__':
unittest.main()
Solution 1:[1]
You need to separate your CI/CD process into two consecutive steps - one for running tests, and another one for deployment. If the tests step fails (i.e, executes a command which returns non-zero exit code), that will fail the entire pipeline, preventing the deployment step from execution.
Your bitbucket-pipelines.yml should look similar to this:
definitions:
steps:
- step: &test
script:
- pytest
- step: &deploy
script:
- docker build ...
- docker push ...
pipelines:
branches:
main:
- step: *test
- step: *deploy
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | esimonov |
