Skip to main content

A unit test runner for Python, and generate HTML reports.

Project description

# PyUnitReport

PyUnitReport is a unittest test runner that save test results in Html files, for human readable presentation of results.

## Installation

```bash
$ pip install PyUnitReport
```

## Usage

### testcase

```python
import PyUnitReport
import unittest

class TestStringMethods(unittest.TestCase):
""" Example test for HtmlRunner. """

def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')

def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())

def test_split(self):
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)

def test_error(self):
""" This test should be marked as error one. """
raise ValueError

def test_fail(self):
""" This test should fail. """
self.assertEqual(1, 2)

@unittest.skip("This is a skipped test.")
def test_skip(self):
""" This test should be skipped. """
pass

if __name__ == '__main__':
unittest.main(testRunner=PyUnitReport.HTMLTestRunner(output='example_dir'))
```

In most cases, you can use `PyUnitReport` with `unittest.main`, just pass it with the `testRunner` keyword.

For `HTMLTestRunner`, the only parameter you must pass in is `output`, which specifies the directory of your generated report. Also, if you want to specify the report name, you can use the `report_name` parameter, otherwise the report name will be the datetime you run test. And if you want to run testcases in `failfast` mode, you can pass in a `failfast` parameter and assign it to be True.

Here is another way to run the testcases.

```python
kwargs = {
"output": output_folder_name,
"report_name": report_name,
"failfast": True
}
result = PyUnitReport.HTMLTestRunner(**kwargs).run(task_suite)
```

### testsuite

For those who have `test suites` it works too, just create a runner instance and call the run method with your suite.

Here is an example:

```python
from unittest import TestLoader, TestSuite
from PyUnitReport import HTMLTestRunner
import ExampleTest
import Example2Test

example_tests = TestLoader().loadTestsFromTestCase(ExampleTests)
example2_tests = TestLoader().loadTestsFromTestCase(Example2Test)

suite = TestSuite([example_tests, example2_tests])
kwargs = {
"output": output_folder_name,
"report_name": report_name,
"failfast": True
}
runner = HTMLTestRunner(**kwargs)
runner.run(suite)
```

## Output

### Console output

This is an example of what you got in the console.

```text
$ python examples/testcase.py

Running tests...
----------------------------------------------------------------------
This test should be marked as error one. ... ERROR (0.000575)s
This test should fail. ... FAIL (0.000564)s
test_isupper (__main__.TestStringMethods) ... OK (0.000149)s
This test should be skipped. ... SKIP (0.000067)s
test_split (__main__.TestStringMethods) ... OK (0.000167)s
test_upper (__main__.TestStringMethods) ... OK (0.000134)s

======================================================================
ERROR [0.000575s]: This test should be marked as error one.
----------------------------------------------------------------------
Traceback (most recent call last):
File "examples/testcase.py", line 23, in test_error
raise ValueError
ValueError

======================================================================
FAIL [0.000564s]: This test should fail.
----------------------------------------------------------------------
Traceback (most recent call last):
File "examples/testcase.py", line 27, in test_fail
self.assertEqual(1, 2)
AssertionError: 1 != 2

----------------------------------------------------------------------
Ran 6 tests in 0.002s

FAILED
(Failures=1, Errors=1, Skipped=1)

Generating HTML reports...
Template is not specified, load default template instead.
Reports generated: /Users/Leo/MyProjects/ApiTestEngine/src/pyunitreport/reports/example_dir/2017-07-26-23-33-49.html
```

### Html Output

![html output](docs/html_output.gif)

![html output](docs/html_output.png)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

PyUnitReport-0.1.3a0.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

PyUnitReport-0.1.3a0-py3.6.egg (19.5 kB view details)

Uploaded Source

File details

Details for the file PyUnitReport-0.1.3a0.tar.gz.

File metadata

File hashes

Hashes for PyUnitReport-0.1.3a0.tar.gz
Algorithm Hash digest
SHA256 9a2ce86186014e20787aa4cac2ee033916a15f704b78befb5f9301d7916bff49
MD5 b33288ab888546e7a9fb8182c06a24cc
BLAKE2b-256 5b71e01234ba633ffa19f2a9c96cf695329fdc2a4207a275704514c2273fd6f7

See more details on using hashes here.

File details

Details for the file PyUnitReport-0.1.3a0-py3.6.egg.

File metadata

File hashes

Hashes for PyUnitReport-0.1.3a0-py3.6.egg
Algorithm Hash digest
SHA256 dbca5e46c2cd682d5bfe7dfcce5a50085ea2d19aebba39058f085d2c39ee083a
MD5 97c313338e98c85d8eee3bce7da2115e
BLAKE2b-256 e15e518c0ffe73aa6f2743af41250e3c97bdcc887d6c55c8409085a1f1a5d670

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page