---
title: "Test Case Writing Guidelines and Examples - Python Unittest"
language: "en"
url: "https://docs.testany.io/en/docs/test-case-writing-guidelines-and-examples-python-u/"
markdown_url: "https://docs.testany.io/en/docs/test-case-writing-guidelines-and-examples-python-u.md"
translation: "https://docs.testany.io/zh/docs/test-case-writing-guidelines-and-examples-python-u.md"
last_updated: "2026-04-21T15:45:20+08:00"
---

# Test Case Writing Guidelines and Examples - Python Unittest

Python [unittest](https://docs.python.org/3/library/unittest.html) 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.


> **Info**
>
> 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()
   ```


> **Tip**
>
> **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 page** → **Environment 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](https://docs.testany.io/en/docs/managing-test-case.md) for how to configure environment variables on the platform.


2. `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

   ```text
   requests
   ```

zip all the related files as one zip file, in this case the test case package file is
[python-test.zip](../../../__attachments/24641637/python-test.zip?inst-v=07864bc4-715d-4f60-b8f8-0d2f7bf42d59)

![](https://docs.testany.io/docs/images/en/image-20230903-053816_33948e9d.png)

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 [<Managing Test Case>](https://docs.testany.io/en/docs/managing-test-case.md) for the registration step-by-step guide.

×

## Glossary terms on this page

- [Test Case](https://docs.testany.io/en/docs/glossary.md#test-case)
- [Executor](https://docs.testany.io/en/docs/glossary.md#executor)
- [Command](https://docs.testany.io/en/docs/glossary.md#command)
- [Environment](https://docs.testany.io/en/docs/glossary.md#environment)
- [Test Script](https://docs.testany.io/en/docs/glossary.md#test-script)
