Testé avec Python

doctest

Peut être écrit en docstring. Peut être utilisé comme légende.

Comment utiliser

Importez doctest, écrivez ce qui suit dans docstring et appelez doctest.testmod () ''. Lors de l'exécution, utilisez python $ {filename} .py -v``.

Exemple de code

sample.py


# -*- coding: utf-8 -*-

import doctest

def twice(n):
    """Une fonction qui double l'argument et renvoie
    >>> twice(8)
    16
    >>> twice(1850923)
    3701846
    """
    return n * 2

if __name__ == "__main__":
    doctest.testmod()

Courir

python sample.py -v

résultat

Trying:
    twice(8)
Expecting:
    16
ok
Trying:
    twice(1850923)
Expecting:
    3701846
ok
1 items had no tests:
    __main__
1 items passed all tests:
   2 tests in __main__.twice
2 tests in 2 items.
2 passed and 0 failed.
Test passed.

Lire la docstring

python
import sample
help(sample.twice)
twice(n)
Une fonction qui double l'argument et renvoie
    >>> twice(8)
    16
    >>> twice(1850923)
    3701846

C'est ... merci ...

** Lors du test de la méthode d'instance **

sample.py


# -*- coding: utf-8 -*-

class IntUtil:
    def twice(self, n):
        """Une fonction qui double l'argument et renvoie
        >>> mine.twice(8)
        16
        >>> mine.twice(1850923)
        3701846
        """
        return n * 2

if __name__ == "__main__":
    import doctest
    doctest.testmod(extraglobs = {'mine': IntUtil()})

unittest

Le test unitaire général et le test d'intégration sont effectués dans ce module

Démarrer le traitement et terminer le traitement

setUp: exécuter au démarrage tearDown: courir à la fin

functions

Nom de la fonction La description
assertEqual(a, b, msg=None) a == b
assertNotEqual(a, b, msg=None) a != b
assertTrue(expr, msg=None) bool(x) is True
assertFalse(x) bool(x) is False
assertIs(a, b) a is b
assertIsNot(a, b) a is not b
assertIsNone(x) a is None
assertIsNotNone(x) a is not None
assertIn(a, b) a in b
assertNotIn(a, b) a not in b
assertIsInstance(a, b) isinstance(a, b)
assertNotIsInstance(a, b) not isinstance(a, b)

sample_test.py


# -*- coding: utf-8 -*-

import unittest
import sample

class SampleTest(unittest.TestCase):
    def setUp(self):
        self.util = sample.IntUtil()
    def test_twice(self):
        #Est-ce la même valeur que le nombre ajouté?
        import random
        roop = 0
        while roop != 10000:
            n = random.randint(0, 100)
            ans = n + n
            self.assertEqual(self.util.twice(n), ans)
            roop += 1

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

Matériel de référence

Recommended Posts

Testé avec Python
FizzBuzz en Python3
Grattage avec Python
Statistiques avec python
Grattage avec Python
Twilio avec Python
Intégrer avec Python
Jouez avec 2016-Python
AES256 avec python
python commence par ()
avec syntaxe (Python)
Bingo avec python
Zundokokiyoshi avec python
Excel avec Python
Micro-ordinateur avec Python
Cast avec python
Communication série avec Python
Django 1.11 a démarré avec Python3.6
Jugement des nombres premiers avec Python
Python avec eclipse + PyDev.
Communication de socket avec Python
Analyse de données avec python 2
Grattage en Python (préparation)
Essayez de gratter avec Python.
Apprendre Python avec ChemTHEATER 03
"Orienté objet" appris avec python
Exécutez Python avec VBA
Manipuler yaml avec python
Résolvez AtCoder 167 avec python
Communication série avec python
[Python] Utiliser JSON avec Python
Apprendre Python avec ChemTHEATER 05-1
Apprenez Python avec ChemTHEATER
Exécutez prepDE.py avec python3
1.1 Premiers pas avec Python
Collecter des tweets avec Python
Binarisation avec OpenCV / Python
3. 3. Programmation IA avec Python
Méthode Kernel avec Python
Non bloquant avec Python + uWSGI
Grattage avec Python + PhantomJS
Publier des tweets avec python
Conduisez WebDriver avec python
Testé avec boto3 + mock
Utiliser mecab avec Python 3
[Python] Redirection avec CGIHTTPServer
Analyse vocale par python
Pensez à yaml avec python
Premiers pas avec Python
Utiliser DynamoDB avec Python
Getter Zundko avec python
Gérez Excel avec python
Loi d'Ohm avec Python
Jugement des nombres premiers avec python
Exécutez Blender avec python
Résoudre des maths avec Python
Python à partir de Windows 7
Carte thermique par Python + matplotlib
Multi-processus de manière asynchrone avec python
Programmation Python avec Atom
Apprendre Python avec ChemTHEATER 02