Ninja23
Ninja23

Reputation: 82

Make unittest failures cause build failure in Google Cloud build

I am setting up CI/CD on a Python project running on Google Cloud Platform. The code deployment works fine, but I also created some unit tests for the separate functions. These unit tests are in a few different files, so I created a new file that takes all these unit tests and add them to a test suite. For example, I have a file called testFunction1.py as follows:

from main import Function1

class TransformTests(unittest.TestCase):

    def test_function(self):
        # Test goes here


if __name__ == '__main__':
    logging.getLogger().setLevel(logging.ERROR)
    unittest.main()

Then I have a similar file called testFunction2.py for a different function. Finally I have a file called executeTests.py as follows:

import unittest

testmodules = [
    'testFunction1',
    'testFunction2'
]

suite = unittest.TestSuite()
loader = unittest.TestLoader()

for t in testmodules:
    mod = __import__(t, globals(), locals(), ['suite'])
    suite.addTests(loader.loadTestsFromModule(mod))

unittest.TextTestRunner().run(suite)

Now I am running this file to execute all the tests at once and this also works great. My problem is to run this file in Google Cloud Build. My cloudbuild.yaml file is structured as follows:

steps:
  - name: "docker.io/library/python:3.7"
    args: ["pip3", "install", "-t", "/workspace/lib", "-r", "requirements.txt"]
  - name: "docker.io/library/python:3.7"
    args: ["python3", "executeTests.py"]
    env: ["PYTHONPATH=/workspace/lib"]

This works fine, but if a test fails, the cloud build still passes which is of course not what I want.

Does anyone know how I can change this setup so that Cloud build will fail when a test fails? Also this is a simple example, in the real repo I have about 50 test files, so I don't want to add each of them to the cloudbuild.yaml file.

Upvotes: 4

Views: 974

Answers (2)

Ninja23
Ninja23

Reputation: 82

Thanks to guillaume blaquire's answer, I fixed the issue by replacing the last line of code in the executeTests.py file by this:

ret = not unittest.TextTestRunner().run(suite).wasSuccessful()
sys.exit(ret)

Upvotes: 1

guillaume blaquiere
guillaume blaquiere

Reputation: 75705

Cloud Build considers a step OK if the return code is 0. If not, the build fails.

So, in your test code, perform an exit(1) when at least one test fails. The build will stop.

Upvotes: 4

Related Questions