Use test splitting with Python Django tests

To utilize test splitting with CircleCI we require that you pass in a list of tests to run. However, with how you execute tests with Django you are unable to simply glob the tests and pass them in.

Someone shared the issues they ran into, and a solution, in the following Discuss post:

https://discuss.circleci.com/t/python-django-tests-not-being-split-correctly/36624

Using the above, here is a quick example of how to accomplish test splitting:

- run:
    command: |
      # get test files while ignoring __init__ files
      TESTFILES=$(circleci tests glob "catalog/tests/*.py" | sed 's/\S\+__init__.py//g')
      echo $TESTFILES | tr ' ' '\n' | sort | uniq > circleci_test_files.txt
      cat circleci_test_files.txt
      TESTFILES=$(circleci tests split --split-by=timings circleci_test_files.txt)
      # massage filepaths into format manage.py test accepts
      TESTFILES=$(echo $TESTFILES | tr "/" "." | sed 's/.py//g')
      echo $TESTFILES
      pipenv run python manage.py test --verbosity=2 $TESTFILES  
Was this article helpful?
1 out of 1 found this helpful

Comments

0 comments

Article is closed for comments.