Module src.test.python.test_motor_bidirectional

Expand source code
import unittest

from src.main.python.motor_bidirectional import MotorBidirectional


class TestMotorBidirectional(unittest.TestCase):
    motor = MotorBidirectional(88, 99)

    def test_create(self):
        m2 = MotorBidirectional(88, 99)
        self.assertEqual(self.motor.is_running(), m2.is_running())
        m3 = MotorBidirectional(77, 88, running=True)
        self.assertTrue(m3.is_running())
        m3 = MotorBidirectional(77, 88, running=False)
        self.assertFalse(m3.is_running())

    def test_start(self):
        self.motor.on()
        self.assertTrue(self.motor.is_running())
        self.motor.off()
        self.assertFalse(self.motor.is_running())
        self.motor.on()
        self.assertTrue(self.motor.is_running())

    def test_direction(self):
        m5 = MotorBidirectional(66, 88, running=True, direction=1)
        self.assertEqual(1, m5.get_direction())
        m5.on_direction(1)
        self.assertTrue(m5.running)
        self.assertEqual(1, m5.get_direction())
        m5.on_direction(2)
        self.assertTrue(m5.running)
        m5.off()
        self.assertFalse(m5.is_running())
        self.assertEqual(2, m5.get_direction())
        m5.on_direction(1)
        self.assertTrue(m5.running)
        self.assertEqual(1, m5.get_direction())

    def test_pin_direction(self):
        m6 = MotorBidirectional(pin1_num=66, pin2_num=88, running=True, direction=2)
        self.assertEqual(2, m6.get_direction())
        m6.on_pin(66)
        self.assertTrue(m6.running)
        self.assertEqual(1, m6.get_direction())
        m6.on_pin(88)
        self.assertTrue(m6.running)
        self.assertEqual(2, m6.get_direction())
        m6.off()
        self.assertFalse(m6.is_running())
        self.assertEqual(2, m6.get_direction())
        m6.on_pin(88)
        self.assertTrue(m6.running)
        self.assertEqual(2, m6.get_direction())

    def test_print(self):
        m3 = MotorBidirectional(66, 88, running=True, direction=2)
        m3.on()
        m3.off()
        print(m3)
        print(self.motor)


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

Classes

class TestMotorBidirectional (methodName='runTest')

A class whose instances are single test cases.

By default, the test code itself should be placed in a method named 'runTest'.

If the fixture may be used for many test cases, create as many test methods as are needed. When instantiating such a TestCase subclass, specify in the constructor arguments the name of the test method that the instance is to execute.

Test authors should subclass TestCase for their own tests. Construction and deconstruction of the test's environment ('fixture') can be implemented by overriding the 'setUp' and 'tearDown' methods respectively.

If it is necessary to override the init method, the base class init method must always be called. It is important that subclasses should not change the signature of their init method, since instances of the classes are instantiated automatically by parts of the framework in order to be run.

When subclassing TestCase, you can set these attributes: * failureException: determines which exception will be raised when the instance's assertion methods fail; test methods raising this exception will be deemed to have 'failed' rather than 'errored'. * longMessage: determines whether long messages (including repr of objects used in assert methods) will be printed on failure in addition to any explicit message passed. * maxDiff: sets the maximum length of a diff in failure messages by assert methods using difflib. It is looked up as an instance attribute so can be configured by individual tests if required.

Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.

Expand source code
class TestMotorBidirectional(unittest.TestCase):
    motor = MotorBidirectional(88, 99)

    def test_create(self):
        m2 = MotorBidirectional(88, 99)
        self.assertEqual(self.motor.is_running(), m2.is_running())
        m3 = MotorBidirectional(77, 88, running=True)
        self.assertTrue(m3.is_running())
        m3 = MotorBidirectional(77, 88, running=False)
        self.assertFalse(m3.is_running())

    def test_start(self):
        self.motor.on()
        self.assertTrue(self.motor.is_running())
        self.motor.off()
        self.assertFalse(self.motor.is_running())
        self.motor.on()
        self.assertTrue(self.motor.is_running())

    def test_direction(self):
        m5 = MotorBidirectional(66, 88, running=True, direction=1)
        self.assertEqual(1, m5.get_direction())
        m5.on_direction(1)
        self.assertTrue(m5.running)
        self.assertEqual(1, m5.get_direction())
        m5.on_direction(2)
        self.assertTrue(m5.running)
        m5.off()
        self.assertFalse(m5.is_running())
        self.assertEqual(2, m5.get_direction())
        m5.on_direction(1)
        self.assertTrue(m5.running)
        self.assertEqual(1, m5.get_direction())

    def test_pin_direction(self):
        m6 = MotorBidirectional(pin1_num=66, pin2_num=88, running=True, direction=2)
        self.assertEqual(2, m6.get_direction())
        m6.on_pin(66)
        self.assertTrue(m6.running)
        self.assertEqual(1, m6.get_direction())
        m6.on_pin(88)
        self.assertTrue(m6.running)
        self.assertEqual(2, m6.get_direction())
        m6.off()
        self.assertFalse(m6.is_running())
        self.assertEqual(2, m6.get_direction())
        m6.on_pin(88)
        self.assertTrue(m6.running)
        self.assertEqual(2, m6.get_direction())

    def test_print(self):
        m3 = MotorBidirectional(66, 88, running=True, direction=2)
        m3.on()
        m3.off()
        print(m3)
        print(self.motor)

Ancestors

  • unittest.case.TestCase

Class variables

var motor

Methods

def test_create(self)
Expand source code
def test_create(self):
    m2 = MotorBidirectional(88, 99)
    self.assertEqual(self.motor.is_running(), m2.is_running())
    m3 = MotorBidirectional(77, 88, running=True)
    self.assertTrue(m3.is_running())
    m3 = MotorBidirectional(77, 88, running=False)
    self.assertFalse(m3.is_running())
def test_direction(self)
Expand source code
def test_direction(self):
    m5 = MotorBidirectional(66, 88, running=True, direction=1)
    self.assertEqual(1, m5.get_direction())
    m5.on_direction(1)
    self.assertTrue(m5.running)
    self.assertEqual(1, m5.get_direction())
    m5.on_direction(2)
    self.assertTrue(m5.running)
    m5.off()
    self.assertFalse(m5.is_running())
    self.assertEqual(2, m5.get_direction())
    m5.on_direction(1)
    self.assertTrue(m5.running)
    self.assertEqual(1, m5.get_direction())
def test_pin_direction(self)
Expand source code
def test_pin_direction(self):
    m6 = MotorBidirectional(pin1_num=66, pin2_num=88, running=True, direction=2)
    self.assertEqual(2, m6.get_direction())
    m6.on_pin(66)
    self.assertTrue(m6.running)
    self.assertEqual(1, m6.get_direction())
    m6.on_pin(88)
    self.assertTrue(m6.running)
    self.assertEqual(2, m6.get_direction())
    m6.off()
    self.assertFalse(m6.is_running())
    self.assertEqual(2, m6.get_direction())
    m6.on_pin(88)
    self.assertTrue(m6.running)
    self.assertEqual(2, m6.get_direction())
def test_print(self)
Expand source code
def test_print(self):
    m3 = MotorBidirectional(66, 88, running=True, direction=2)
    m3.on()
    m3.off()
    print(m3)
    print(self.motor)
def test_start(self)
Expand source code
def test_start(self):
    self.motor.on()
    self.assertTrue(self.motor.is_running())
    self.motor.off()
    self.assertFalse(self.motor.is_running())
    self.motor.on()
    self.assertTrue(self.motor.is_running())