Test Case Writing Guidelines and Examples - Python Unittest

Python unittest is a python unit test framework. Testany support this popular python test framework. This document shows how to writing a python unittest test case in Testany.

ℹ️

Testany test case supported python version is 3.11.5, it should compatible other lower python versions. If a new python version needed for any specific reason, please feel free to contact our Customer Support

Test case package - one zip file

Currently we only support uploading your local zip file to Testany. The zip file should follow this structure:

my-python-test.zip
├── test_api.py          ← Main test file (required)
├── utils.py             ← Helper modules (optional)
└── requirements.txt     ← Dependencies (optional, auto-installed)

Registration settings:

  • Executor: Python
  • Command: python -m pytest test_api.py (or python test_api.py for unittest)

The zip file needs to contain at least one file:

  1. Test case code file or files, like app.py

    Python
    import unittest
    import os
    
    class TestStringMethods(unittest.TestCase):
    
        def test_upper(self):
            test_str = os.getenv('TEST_STR')
            self.assertEqual('foo'.upper(), test_str)
    
        def test_isupper(self):
            self.assertTrue('FOO'.isupper())
            self.assertFalse('Foo'.isupper())
    
        def test_split(self):
            s = os.getenv('SPLIT_STR')
            #s = 'hello world'
            self.assertEqual(s.split(), ['hello', 'world'])
            # check that s.split fails when the separator is not a string
            with self.assertRaises(TypeError):
                s.split(2)
    
    if __name__ == '__main__':
        unittest.main()
💡

Environment Variables Configuration

The environment variables used in your code (e.g., TEST_STR, SPLIT_STR) must be configured in both places:

  1. Test Case detail pageEnvironment Variables section: Define the variable name and value
  2. Test script code: Use os.getenv('VARIABLE_NAME') to read the value

See Managing Test Case for how to configure environment variables on the platform.

  1. requirements.txt file if your python script use some specific libraries, you can create a requirements.txt file in your root folder, Testany will auto download these libraries in python execute environment. requirements.txt example

    Plain Text
    requests

zip all the related files as one zip file, in this case the test case package file is python-test.zip

After you've successfully prepared the .zip file, you will be able to go to register your test case on Testany Platform. Please refer to for the registration step-by-step guide.

×

Still have questions?

Our team is here to help. Get in touch and we'll get back to you as soon as possible.