All Projects → google → Pytruth

google / Pytruth

Licence: apache-2.0
Assertion framework for Python unit tests

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Pytruth

Unit Threaded
Advanced unit test framework for D
Stars: ✭ 100 (-15.25%)
Mutual labels:  mock, unittest
emock
🐞 下一代C/C++跨平台mock库 (Next generation cross-platform mock library for C/C++)
Stars: ✭ 73 (-38.14%)
Mutual labels:  mock, unittest
Fake Xrm Easy
The testing framework for Dynamics CRM and Dynamics 365 which runs on an In-Memory context and deals with mocks or fakes for you
Stars: ✭ 216 (+83.05%)
Mutual labels:  mock, unittest
Swiftymocky
Framework for automatic mock generation. Adds a set of handy methods, simplifying testing. One of the best and most complete solutions, including generics support and much more.
Stars: ✭ 748 (+533.9%)
Mutual labels:  mock, unittest
local-data-api
Data API for local, you can write unittest for AWS Aurora Serverless's Data API
Stars: ✭ 99 (-16.1%)
Mutual labels:  mock, unittest
Testable Mock
换种思路写Mock,让单元测试更简单
Stars: ✭ 977 (+727.97%)
Mutual labels:  mock, unittest
Bulma Fluent
Fluent Design Theme for Bulma, inspired by Microsoft’s Fluent Design System. https://mubaidr.github.io/bulma-fluent/
Stars: ✭ 101 (-14.41%)
Mutual labels:  fluent
Mockssh
Mock an SSH server and define all commands it supports (Python, Twisted)
Stars: ✭ 107 (-9.32%)
Mutual labels:  mock
Interceptors
Low-level HTTP/HTTPS/XHR/fetch request interception library.
Stars: ✭ 100 (-15.25%)
Mutual labels:  mock
Unittesting
Testing Sublime Text Packages
Stars: ✭ 95 (-19.49%)
Mutual labels:  unittest
Vue Spa Project
vue.js + vuex + vue-router + fetch + element-ui + es6 + webpack + mock 纯前端SPA项目开发实践
Stars: ✭ 118 (+0%)
Mutual labels:  mock
Parrot
✨ Scenario-based HTTP mocking
Stars: ✭ 109 (-7.63%)
Mutual labels:  mock
Prig
Prig is a lightweight framework for test indirections in .NET Framework.
Stars: ✭ 106 (-10.17%)
Mutual labels:  mock
Mockery
Mockery is a simple yet flexible PHP mock object framework for use in unit testing with PHPUnit, PHPSpec or any other testing framework. Its core goal is to offer a test double framework with a succinct API capable of clearly defining all possible object operations and interactions using a human readable Domain Specific Language (DSL).
Stars: ✭ 10,048 (+8415.25%)
Mutual labels:  mock
Impersonator
Ruby library to record and replay object interactions
Stars: ✭ 100 (-15.25%)
Mutual labels:  mock
Rabbitmq Mock
Mock for RabbitMQ Java amqp-client
Stars: ✭ 114 (-3.39%)
Mutual labels:  mock
Cuckoo
Boilerplate-free mocking framework for Swift!
Stars: ✭ 1,344 (+1038.98%)
Mutual labels:  mock
React Nativeish
React Native / React Native Web Boilerplate
Stars: ✭ 106 (-10.17%)
Mutual labels:  mock
Phprap
PHPRAP,是一个PHP轻量级开源API接口文档管理系统,致力于减少前后端沟通成本,提高团队协作开发效率,打造PHP版的RAP。如果您觉得PHPRAP对您有用的话,别忘了给点个赞哦^_^ !
Stars: ✭ 527 (+346.61%)
Mutual labels:  mock
Mimic
A mocking library for Elixir
Stars: ✭ 104 (-11.86%)
Mutual labels:  mock

PyTruth: Truth in Python

Development Status Build Status PyPI Version Python Versions

Provides unittest assertions in a fluent style. Translated from the Java implementation, google/truth.

License

PyTruth is licensed under the Apache 2.0 license.

Disclaimer

PyTruth is not an official Google product.

Contributing

Please see the guidelines for contributing before creating pull requests.

Support

PyTruth is not an actively maintained project. No support is provided.

It is shared with the community to bring an expressive, consistent assertion style to projects that may be using a combination of unittest, abseil, googletest, mox, and mock—especially to people familiar with Java Truth.

User group: [email protected]

Installing

PyTruth can be installed using pip:

pip install pytruth

Overview

Import the truth module and alias the AssertThat() method to begin asserting things:

from truth.truth import AssertThat

Then, instead of writing

self.assertEqual(a, b)
self.assertTrue(c)
self.assertIn(a, d)
self.assertTrue(a in d and b in d)
self.assertTrue(a in d or b in d or c in d)
with self.assertRaises(Error):
  Explode()

one would write

AssertThat(a).IsEqualTo(b)
AssertThat(c).IsTrue()
AssertThat(d).Contains(a)
AssertThat(d).ContainsAllOf(a, b)
AssertThat(d).ContainsAnyOf(a, b, c)
with AssertThat(Error).IsRaised():
  Explode()

Tests should be easier to read and write, and flow more clearly.

Limitations

unittest assertions accept a msg parameter to display if the assertion fails. PyTruth has no such mechanism, though its failure messages tend to be more informative.

The type of the subject under test (the parameter passed to AssertThat()) will not be known until runtime, unlike Java where the type is known at compile time. IDEs may not correctly autocomplete available predicates on an asserted subject.

In Python 2, None compares less than every other thing, except None itself. None is less than nan, and it is less than negative infinity. Therefore, use caution when a function might return None. The assertion AssertThat(Func()).IsLessThan(0) succeeds whether Func() returns a negative number or None. Instead, first check the None-ness of the return value with IsNone() or IsNotNone() before performing an inequality assertion.

In Python 3, None is no longer comparable using < > <= >=. PyTruth detects the version of the Python interpreter and compares or fails appropriately, rather than allowing Python 3's TypeError to bubble up.

If the iterator over a shared value (either expected or actual) changes that value or its underlying elements, the behavior is undefined: all, none, or some of the assertions may succeed or fail, arbitrarily.

This library is threadsafe; you may execute multiple assertions in parallel.

Conversion Recipes

General

unittest PyTruth
assertEqual(a, b) AssertThat(a).IsEqualTo(b)
assertNotEqual(a, b) AssertThat(a).IsNotEqualTo(b)
assertTrue(a) AssertThat(a).IsTruthy()
assertFalse(a) AssertThat(a).IsFalsy()
assertIs(a, True) AssertThat(a).IsTrue()
assertIs(a, False) AssertThat(a).IsFalse()
assertIs(a, b) AssertThat(a).IsSameAs(b)
assertIsNot(a, b) AssertThat(a).IsNotSameAs(b)
assertIsNone(a) AssertThat(a).IsNone()
assertIsNotNone(a) AssertThat(a).IsNotNone()
assertIn(a, b) AssertThat(a).IsIn(b)
assertIn(a, [b, c, d]) AssertThat(a).IsAnyOf(b, c, d)
assertNotIn(a, b) AssertThat(a).IsNotIn(b)
assertNotIn(a, [b, c, d]) AssertThat(a).IsNoneOf(b, c, d)
assertIsInstance(a, b) AssertThat(a).IsInstanceOf(b)
assertIsNotInstance(a, b) AssertThat(a).IsNotInstanceOf(b)
assertTrue(hasattr(a, b)) AssertThat(a).HasAttribute(b)
assertFalse(hasattr(a, b)) AssertThat(a).DoesNotHaveAttribute(b)
assertTrue(callable(a)) AssertThat(a).IsCallable()
assertFalse(callable(a)) AssertThat(a).IsNotCallable()

Truthiness

PyTruth supports a finer grained distinction of truthiness than unittest does. In particular, it differentiates between "is True" and "is truthy." unittest's assertTrue(x) is equivalent to assertIs(bool(x), True), and its assertFalse(x) is equivalent to assertIs(bool(x), False). PyTruth's IsTrue() and IsFalse() predicates match only the boolean subjects True and False themselves. Therefore it is important not to blindly convert assertTrue() to IsTrue(), and likewise with assertFalse() and IsFalse().

Truthy assertion Result Falsy assertion Result
assertTrue(True) succeeds assertFalse(False) succeeds
assertTrue(1) succeeds assertFalse(0) succeeds
assertTrue(None) fails assertFalse(None) succeeds
AssertThat(True).IsTrue() succeeds AssertThat(False).IsFalse() succeeds
AssertThat(1).IsTrue() fails AssertThat(0).IsFalse() fails
AssertThat(None).IsTrue() fails AssertThat(None).IsFalse() fails
AssertThat(True).IsTruthy() succeeds AssertThat(False).IsFalsy() succeeds
AssertThat(1).IsTruthy() succeeds AssertThat(0).IsFalsy() succeeds
AssertThat(None).IsTruthy() fails AssertThat(None).IsFalsy() succeeds

Strings

unittest PyTruth
assertEqual(len(s), n) AssertThat(s).HasLength(n)
assertTrue(s.startswith('a')) AssertThat(s).StartsWith('a')
assertTrue(s.endswith('a')) AssertThat(s).EndsWith('a')
assertRegex(s, r)
assertRegexpMatches(s, r)
AssertThat(s).ContainsMatch(r)
assertNotRegex(s, r)
assertNotRegexpMatches(s, r)
AssertThat(s).DoesNotContainMatch(r)
assertRegex(s, '^r')
assertRegexpMatches(s, '^r')
AssertThat(s).Matches('r')
assertNotRegex(s, '^r')
assertNotRegexpMatches(s, '^r')
AssertThat(s).DoesNotMatch('r')

Matching strings

The r parameter passed to the matching functions may either be a r'raw string', or a pattern object returned from re.compile().

Numbers, strings, and other comparable things

unittest PyTruth
assertLess(a, b) AssertThat(a).IsLessThan(b)
assertGreater(a, b) AssertThat(a).IsGreaterThan(b)
assertLessEqual(a, b) AssertThat(a).IsAtMost(b)
assertGreaterEqual(a, b) AssertThat(a).IsAtLeast(b)

Numbers

unittest PyTruth
assertEqual(a, 0) AssertThat(a).IsZero()
assertNotEqual(a, 0) AssertThat(a).IsNonZero()
assertEqual(a, float('inf')) AssertThat(a).IsPositiveInfinity()
assertEqual(a, float('-inf')) AssertThat(a).IsNegativeInfinity()
assertFalse(a.isinf() or a.isnan()) AssertThat(a).IsFinite()
assertTrue(a.isinf() or a.isnan()) AssertThat(a).IsNotFinite()
assertTrue(a.isnan()) AssertThat(a).IsNan()
assertFalse(a.isnan()) AssertThat(a).IsNotNan()
assertAlmostEqual(a, b, delta=d) AssertThat(a).IsWithin(d).Of(b)
assertNotAlmostEqual(a, b, delta=d) AssertThat(a).IsNotWithin(d).Of(b)

Lists, strings, and other iterables

unittest PyTruth
assertEqual(len(a), n) AssertThat(a).HasSize(n)
assertEqual(len(a), 0) AssertThat(a).IsEmpty()
assertGreaterThan(len(a), 0) AssertThat(a).IsNotEmpty()
assertIn(b, a) AssertThat(a).Contains(b)
assertNotIn(b, a) AssertThat(a).DoesNotContain(b)
assertTrue(b in a and c in a) AssertThat(a).ContainsAllOf(b, c)
AssertThat(a).ContainsAllIn([b, c])
assertTrue(b in a or c in a) AssertThat(a).ContainsAnyOf(b, c)
AssertThat(a).ContainsAnyIn([b, c])
assertTrue(b in a and c in a and len(a) == 2) AssertThat(a).ContainsExactly(b, c)
assertCountEqual(a, b)
assertItemsEqual(a, b)
AssertThat(sorted(a)).ContainsExactlyElementsIn(sorted(b)).InOrder()
assertTrue(b not in a and c not in a) AssertThat(a).ContainsNoneOf(b, c)
AssertThat(a).ContainsNoneIn([b, c])
N/A AssertThat(a).ContainsNoDuplicates()
N/A AssertThat(a).IsOrdered()
N/A AssertThat(a).IsOrderedAccordingTo(cf)
N/A AssertThat(a).IsStrictlyOrdered()
N/A AssertThat(a).IsStrictlyOrderedAccordingTo(cf)
absltest PyTruth
assertLen(a, n) AssertThat(a).HasSize(n)
assertEmpty(a) AssertThat(a).IsEmpty()
assertNotEmpty(a) AssertThat(a).IsNotEmpty()

Defining order

The cf parameter passed to IsOrderedAccordingTo() and IsStrictlyOrderedAccordingTo() should be a callable that follows the contract of cmp(x, y): it should return negative if x < y, zero if x == y, and positive if x > y.

Ordered means that the iterable's elements must increase (or decrease, depending on cf) from beginning to end. Adjacent elements are allowed to be equal. Strictly ordered means that in addition, the elements must be unique (i.e., monotonically increasing or decreasing).

IsOrdered() is equivalent to IsOrderedAccordingTo(cmp).

IsStrictlyOrdered() is equivalent to IsStrictlyOrderedAccordingTo(cmp).

Asserting order

By default, ContainsAll... and ContainsExactly... do not enforce that the order of the elements in the subject under test matches the that of the expected value. To do that, append InOrder() to the returned predicate.

Containment assertion Result
AssertThat([2, 4, 6]).ContainsAllOf(6, 2) succeeds
AssertThat([2, 4, 6]).ContainsAllOf(6, 2).InOrder() fails
AssertThat([2, 4, 6]).ContainsAllOf(2, 6).InOrder() succeeds
AssertThat([2, 4, 6]).ContainsExactly(2, 6, 4) succeeds
AssertThat([2, 4, 6]).ContainsExactly(2, 6, 4).InOrder() fails
AssertThat([2, 4, 6]).ContainsExactly(2, 4, 6).InOrder() succeeds

When using InOrder(), ensure that both the subject under test and the expected value have a predictable order, otherwise the result is undefined. For example, AssertThat(list_a).ContainsExactlyElementsIn(set_a).InOrder() may or may not succeed, depending on how the set implements ordering.

Dictionaries, in addition to the table above

unittest PyTruth
assertIn(k, d) AssertThat(d).ContainsKey(k)
assertNotIn(k, d) AssertThat(d).DoesNotContainKey(k)
assertIn((k, v), d.items()) AssertThat(d).ContainsItem(k, v)
assertNotIn((k, v), d.items()) AssertThat(d).DoesNotContainItem(k, v)
assertEqual(d, {k1: v1, k2: v2}) AssertThat(d).ContainsExactly(k1, v1, k2, v2)
assertEqual(d1, d2) AssertThat(d1).ContainsExactlyItemsIn(d2)
assertDictContainsSubset(d1, d2) AssertThat(d1.items()).ContainsAllIn(d2.items())

Exceptions

unittest PyTruth
with assertRaises(e): with AssertThat(e).IsRaised():
with assertRaisesRegex(e, r): with AssertThat(e).IsRaised(matching=r):
N/A with AssertThat(e).IsRaised(containing='a'):
assertEqual(e.message, m) AssertThat(e).HasMessage(m)
assertTrue(e.message.startswith('a')) AssertThat(e).HasMessageThat().StartsWith('a')
assertIn(a, e.args) AssertThat(e).HasArgsThat().Contains(a)

Matching raised exceptions

When expecting an exception using the AssertThat(e).IsRaised() context, any exception raised whose type is equal to e or a subclass of e is accepted. If an exception is raised that is not a subclass of e, the assertion fails.

The e parameter in the AssertThat(e).IsRaised() context may be either an exception class like ValueError, or it may be an exception instance like ValueError('some error'). If an instance is passed, then any exception raised by the code under test must also have matching message and args properties, in addition to being a subclass of the expected exception.

Mocked functions

unittest PyTruth
m.assert_called() AssertThat(m).WasCalled()
m.assert_not_called() AssertThat(m).WasNotCalled()
m.assert_called_once() AssertThat(m).WasCalled().Once()
assertEqual(m.call_count, n) AssertThat(m).WasCalled().Times(n)
m.assert_called_with(*a, **k) AssertThat(m).WasCalled().LastWith(*a, **k)
m.assert_called_once_with(*a, **k) AssertThat(m).WasCalled().Once().With(*a, **k)
N/A AssertThat(m).WasCalled().With(*a, **k).Once()
m.assert_has_calls(calls, any_order=True) AssertThat(m).HasCalls(calls)
m.assert_has_calls(calls, any_order=False) AssertThat(m).HasCalls(calls).InOrder()
N/A AssertThat(m).HasExactlyCalls(c1, c2)
N/A AssertThat(m).HasExactlyCalls(c1, c2).InOrder()
m.assert_any_call(*a, **k) AssertThat(m).WasCalled().With(*a, **k)

Being called once, with arguments

The WasCalled().Once().With(...) and WasCalled().With(...).Once() assertions are subtly different. WasCalled().Once().With(...) asserts that the function was called one time ever, and that one time it was called, it was passed those arguments. WasCalled().With(...).Once() asserts that the function was passed those arguments exactly once, but it is permitted to have been called with other, irrelevant arguments. Thus, WasCalled().Once().With(...) is the stricter assertion. Consider using HasExactlyCalls() for more clarity.

Classes

unittest PyTruth
assertTrue(a.issubclass(b)) AssertThat(a).IsSubclassOf(b)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].