• Stars
    star
    146
  • Rank 251,682 (Top 5 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created about 11 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

ApprovalTests for python

ApprovalTests.Python

Contents

Capturing Human Intelligence - ApprovalTests is an open source assertion/verification library to aid testing.
approvaltests is the ApprovalTests port for Python.

For more information see: www.approvaltests.com.

PyPI version Python versions Build Status Build Status Build Status

What can I use ApprovalTests for?

You can use ApprovalTests to verify objects that require more than a simple assert including long strings, large arrays, and complex hash structures and objects. ApprovalTests really shines when you need a more granular look at the test failure. Sometimes, trying to find a small difference in a long string printed to STDOUT is just too hard!
ApprovalTests solves this problem by providing reporters which let you view the test results in one of many popular diff utilities.

Getting Started

What Are Approvals

If you need to gain a better understanding or are new to this concept, start here.

New Projects

If you are starting a new project, we suggest you use the Starter Project. You can just clone this and go. It's great for exercises, katas, and green field projects.

Minimal Example Tutorial

If this is first time approvaltesting in python, consider starting here: Minimal Example Tutorial

Adding to Existing Projects

From pypi:

pip install approvaltests

Overview

Approvals work by comparing the test results to a golden master. If no golden master exists you can create a snapshot of the current test results and use that as the golden master. The reporter helps you manage the golden master.
Whenever your current results differ from the golden master, Approvals will launch an external application for you to examine the differences. Either you will update the master because you expected the changes and they are good, or you will go back to your code and update or roll back your changes to get your results back in line with the golden master.

Example using pytest

from approvaltests.approvals import verify


def test_simple():
    result = "Hello ApprovalTests"
    verify(result)

snippet source | anchor

Install the plugin pytest-approvaltests and use it to select a reporter:

pip install pytest-approvaltests
pytest --approvaltests-use-reporter='PythonNative'

The reporter is used both to alert you to changes in your test output, and to provide a tool to update the golden master. In this snippet, we chose the 'PythonNative' reporter when we ran the tests. For more information about selecting reporters see the documentation

Example using unittest

import unittest

from approvaltests.approvals import verify


class GettingStartedTest(unittest.TestCase):
    def test_simple(self):
        verify("Hello ApprovalTests")


if __name__ == "__main__":
    unittest.main()

snippet source | anchor

This example has the same behaviour as the pytest version, but uses the built-in test framework unittest instead.

Example using CLI

You can invoke a verify() call from the command line. This allows invoking python approvals from any other stack via subprocesses.

Usage

python -m approvaltests --test-id hello --received "hello world!"

or

python -m approvaltests -t hello -r "hello world!"

or

echo "hello world!" | python -m approvaltests -t hello

Argument Definitions

  • --test-id or -t: Test identifier used to name the approved.txt and received.txt files for the test.

  • --received or -r: The output of the program under test (a string) that is passed to the verify method.

    • stdin: Instead of providing a received argument, you may use stdin.

Reporters

Selecting a Reporter

All verify functions take an optional options parameter that can configure reporters (as well as many other aspects).

ApprovalTests.Python comes with a few reporters configured, supporting Linux, Mac OSX, and Windows.

In the example shown below, we pass in an options with a reporter we're selecting directly:

class TestSelectReporterFromClass(unittest.TestCase):
    def test_simple(self):
        verify("Hello", options=Options().with_reporter(report_with_beyond_compare()))

snippet source | anchor

You can also use the GenericDiffReporterFactory to find and select the first diff utility that exists on our system.

An advantage of this method is you can modify the reporters.json file directly to handle your unique system.

class TestSelectReporter(unittest.TestCase):
    def setUp(self):
        self.factory = GenericDiffReporterFactory()

    def test_simple(self):
        verify(
            "Hello", options=Options().with_reporter(self.factory.get("BeyondCompare"))
        )

snippet source | anchor

Or you can build your own GenericDiffReporter on the fly

class GettingStartedTest(unittest.TestCase):
    def test_simple(self):
        verify(
            "Hello",
            options=Options().with_reporter(
                GenericDiffReporter.create(r"C:\my\favorite\diff\utility.exe")
            ),
        )

snippet source | anchor

As long as C:/my/favorite/diff/utility.exe can be invoked from the command line using the format utility.exe file1 file2 then it will be compatible with GenericDiffReporter. Otherwise you will have to derive your own reporter, which we won't cover here.

JSON file for collection of reporters

To wrap things up, I should note that you can completely replace the collection of reporters known to the reporter factory by writing your own JSON file and loading it.

For example if you had C:/myreporters.json

[
    ["BeyondCompare4", "C:/Program Files (x86)/Beyond Compare 4/BCompare.exe"],
    ["WinMerge", "C:/Program Files (x86)/WinMerge/WinMergeU.exe"],
    ["Tortoise", "C:/Program Files (x86)/TortoiseSVN/bin/tortoisemerge.exe"]
]

You could then use that file by loading it into the factory:

import unittest

from approvaltests.approvals import verify
from approvaltests.reporters.generic_diff_reporter_factory import GenericDiffReporterFactory


class GettingStartedTest(unittest.TestCase):
    def setUp(self):
        factory = GenericDiffReporterFactory()
        factory.load('C:/myreporters.json')
        self.reporter = factory.get_first_working()

    def test_simple(self):
        verify('Hello', self.reporter)

if __name__ == "__main__":
    unittest.main()

Of course, if you have some interesting new reporters in myreporters.json then please consider updating the reporters.json file that ships with Approvals and submitting a pull request.

Support and Documentation

Missing Documentation?

If there is documentation you wish existed, please add a page request to this issue.

Dependencies

ApprovalTests require Python 3.7 or greater and the following dependencies:

Required dependencies

These dependencies are always required for approvaltests

pytest>=4.0.0
empty-files>=0.0.3
typing_extensions>=3.6.2

snippet source | anchor

Extra dependencies

These dependencies are required if you are going to use the related functionality If you want the bare minimum you can use the pypi project approvaltests-minimal

pyperclip>=1.5.29     # For Clipboard Reporter
beautifulsoup4>=4.4.0 # For verify_html
allpairspy>=2.1.0     # For PairwiseCombinations
mrjob>=0.7.4          # For MrJob
testfixtures >= 7.1.0 # For verify_logging
mock >= 5.1.0         # For verify_logging

snippet source | anchor

For developers

Weekly Ensemble

The best way to contribute is to join our weekly mob/ensemble

Pull Requests

Pull requests are welcomed, particularly those accompanied by automated tests.

To run the self-tests, install pytest and tox, then execute

python -m tox

This will run the self-tests on several python versions. We support python 3.7 and above.

All pull requests will be pre-checked using GitHub actions to execute all these tests. You can see the results of test runs here.

More Repositories

1

ApprovalTests.Net

ApprovalTest verification library for .Net
C#
569
star
2

ApprovalTests.Java

ApprovalTest verification library for Java
Java
335
star
3

ApprovalTests.cpp

Native ApprovalTests for C++ on Linux, Mac and Windows
C++
316
star
4

ApprovalTests.Ruby

Approval Tests for Ruby
Ruby
228
star
5

Approvals.NodeJS

Approval Tests implementation in NodeJS
TypeScript
104
star
6

ApprovalTests.Swift

ApprovalTests for Swift, a powerful alternative to assertions
Swift
88
star
7

go-approval-tests

Go
86
star
8

ApprovalTests.Net.Json

Extends ApprovalTests to allow simple approval of complex models.
C#
27
star
9

ApprovalTests.php

PHP
25
star
10

approval_tests

Approval testing for specs and unit tests
Ruby
16
star
11

ApprovalTests.cpp.Qt

C++
15
star
12

ApprovalTests.Net.Koans

Helpful examples to learn ApprovalTests
C#
14
star
13

ApprovalTests.Dart

Approval Tests implementation in Dart
Dart
11
star
14

ApprovalTests.cpp.StarterProject

Starter project for easy learning and use of ApprovalTests.cpp
C++
11
star
15

ApprovalTests.java.Koans

Java
8
star
16

ApprovalTests.Net.Asp

Easy ways to test Asp & Aps.Mvc pages and routes
C#
8
star
17

ApprovalTests.Python.StarterProject

Starter project for getting approvaltests up and running
Python
7
star
18

ApprovalTests.Net.Wpf

Extends ApprovalTests for approval of WPF through screenshot verification.
C#
7
star
19

ApprovalTests.java.StarterProject

Sample Maven project to get started
Java
6
star
20

GroupLearningFacilitation

Printouts to help facilitate a randori/mob programming session of koans
5
star
21

ApprovalTests.js.StarterProject

Starter project to get going. Mocha/ApprovalTests
PowerShell
5
star
22

ApprovalTests.perl

ApprovalTests for Perl
Perl
5
star
23

ApprovalTests.shell

a tiny implementation of Approval Tests for the cli
Shell
5
star
24

merb_approvals

Ruby
3
star
25

ApprovalTests.Net.WinForms

Extends ApprovalTests for approval of Windows Forms through screenshot verification.
C#
3
star
26

Approvals.Net.WebApi

C#
3
star
27

ApprovalTests.cpp.Qt.StarterProject

C++
3
star
28

Approvals.Net.Excel

C#
2
star
29

ApprovalTests.Net.StatePrinter

Extends ApprovalTests to allow simple approval of complex models using StatePrinter.
C#
2
star
30

ApprovalTests.Net.StarterProject

Starter Vs2017 project with MsTest & ApprovalTests
C#
2
star
31

ApprovalTests.TypeScript.Jest.StarterProject

TypeScript
2
star
32

Approvaltests.Kotlin.StarterProject

Kotlin
2
star
33

ApprovalTests.java.StarterProject.gradle

Clone and Go
Java
2
star
34

ApprovalTests.Objective-C

ApprovalTest verification library for Objective-C
Objective-C
2
star
35

ApprovalTests.Go.StarterProject

Go
2
star
36

ApprovalTests.Net.Xunit

Simplifies xUnit support in ApprovalTests
C#
2
star
37

ApprovalTests.Net.EntityFramework

Extends ApprovalTests to allow approval of EntityFramework Queries
C#
2
star
38

ApprovalTests.plugins.intellij

IntelliJ Plugin for ApprovalTests.Java
Kotlin
2
star
39

Approvaltests.Dart.StarterProject

Starter Project for Approval Tests implementation in Dart
Dart
2
star
40

Approvaltests.Elixir

Elixir
2
star
41

ApprovalTests.cpp.StarterProject.GoogleTests

VS2017 GoogleTest starter project
C++
1
star
42

ApprovalTests.LabVIEW.StarterProject

LabVIEW
1
star
43

ApprovalTests.Dart.Flutter

Dart
1
star
44

ApprovalTests.Net.NHibernate

C#
1
star
45

PackageSettings.java

Package level settings for java
Java
1
star
46

ApprovalTests.pharo.StarterProject

Smalltalk
1
star
47

ApprovalTests.Documentation

ApprovalTests.Documentation
1
star
48

ApprovalTests.Net.NServiceBus

Adds ApprovalTests support to NServiceBus
C#
1
star
49

notes

1
star
50

ApprovalTests.cpp.StarterProject.VisualStudio

1
star
51

ApprovalTests.Scala.StarterProject

Scala
1
star
52

.github

1
star
53

EmptyFiles.Python

Python
1
star
54

ApprovalTests.Swift.StarterProject.MacOs

Swift
1
star
55

ApprovalTests.Net.Rdlc

C#
1
star
56

ApprovalTests.Groovy.StarterProject

Groovy
1
star
57

go-approval-tests-starter-project

clone and go for golang
PowerShell
1
star
58

ApprovalTests.net.StarterProject.core

C#
1
star
59

ApprovalTests.GlamorousToolkit.StarterProject

Smalltalk
1
star
60

ApprovalTests.Ruby.starterproject

Ruby
1
star
61

ApprovalTests.LabVIEW

LabVIEW
1
star
62

ApprovalTests.Net.Aspose

Extends ApprovalTests to allow approval of documents via Aspose.
C#
1
star
63

ApprovalTests.Net.AppConfig

C#
1
star
64

ApprovalTests.Angular.Jest.starterproject

TypeScript
1
star
65

ApprovalTests.lua

Approvals for lua
Lua
1
star
66

approvals.github.io

SCSS
1
star