Skip to content

Instantly share code, notes, and snippets.

@samdoran
Last active April 11, 2019 21:19
Show Gist options
  • Save samdoran/cf2789ae7cbfce2aec6859e90816dea6 to your computer and use it in GitHub Desktop.
Save samdoran/cf2789ae7cbfce2aec6859e90816dea6 to your computer and use it in GitHub Desktop.
Example pytest unit test
import time
from ansible.module_utils.facts.hardware import sunos
def test_sunos_get_uptime_facts(mocker):
kstat_output = '\nunix:0:system_misc:boot_time\t1548249689\n'
module_mock = mocker.patch('ansible.module_utils.basic.AnsibleModule')
module = module_mock()
module.run_command.return_value = (0, kstat_output, '')
inst = sunos.SunOSHardware(module)
expected = int(time.time()) - 1548249689
result = inst.get_uptime_facts()
assert expected == result['uptime_seconds']
@mator
Copy link

mator commented Apr 9, 2019

tried this one

mator@windrunner:~/ansible$ cat test/units/module_utils/facts/hardware/test.py
from units.compat import unittest
from units.compat.mock import Mock, patch
from ansible.module_utils.facts.hardware import sunos


import mocker


def test_sunos_get_uptime_facts(mocker):
    kstat_output = b'\nunix:0:system_misc:boot_time    1548249689\n'

    module = mocker.Mock()
    inst = sunos.SunOSHardware(module)
    expected = b''  # whatever you expect

    mocker.patch('inst.module.run_command', result=[0, kstat_output, b''])
    rc, out, err = inst.get_uptime_facts()
    assert expected == out



test_sunos_get_uptime_facts

pytest run output

mator@windrunner:~/ansible$ pytest test/units/module_utils/facts/hardware/test.py 
=============================================================== test session starts ================================================================
platform linux2 -- Python 2.7.16, pytest-3.10.1, py-1.7.0, pluggy-0.8.0
rootdir: /home/mator/ansible, inifile: tox.ini
collected 1 item                                                                                                                                   

test/units/module_utils/facts/hardware/test.py E                                                                                             [100%]

====================================================================== ERRORS ======================================================================
__________________________________________________ ERROR at setup of test_sunos_get_uptime_facts ___________________________________________________
file /home/mator/ansible/test/units/module_utils/facts/hardware/test.py, line 9
  def test_sunos_get_uptime_facts(mocker):
E       fixture 'mocker' not found
>       available fixtures: am, cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, pytestconfig, record_property, record_xml_attribute, record_xml_property, recwarn, stdin, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

/home/mator/ansible/test/units/module_utils/facts/hardware/test.py:9
==============================

i'm getting

E       fixture 'mocker' not found

with test/units/module_utils/facts/hardware/test_linux_get_cpu_info.py

mator@windrunner:~/ansible$ pytest test/units/module_utils/facts/hardware/test_linux_get_cpu_info.py 
=============================================================== test session starts ================================================================
platform linux2 -- Python 2.7.16, pytest-3.10.1, py-1.7.0, pluggy-0.8.0
rootdir: /home/mator/ansible, inifile: tox.ini
collected 1 item                                                                                                                                   

test/units/module_utils/facts/hardware/test_linux_get_cpu_info.py E                                                                          [100%]

====================================================================== ERRORS ======================================================================
_______________________________________________________ ERROR at setup of test_get_cpu_info ________________________________________________________
file /home/mator/ansible/test/units/module_utils/facts/hardware/test_linux_get_cpu_info.py, line 13
  def test_get_cpu_info(mocker):
E       fixture 'mocker' not found
>       available fixtures: am, cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, pytestconfig, record_property, record_xml_attribute, record_xml_property, recwarn, stdin, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

/home/mator/ansible/test/units/module_utils/facts/hardware/test_linux_get_cpu_info.py:13
============================================================= 1 error in 0.05 seconds ==============================================================
mator@windrunner:~/ansible$ 

as well

but ansible-test units --tox ... works

PS: installed 'mock' packages

$ dpkg -l | grep mock
ii  python-httmock                   1.3.0-1                             all          Mocking library for python-requests
ii  python-mock                      2.0.0-4                             all          Mocking and Testing Library
ii  python-mocker                    1.0-2                               all          Mocker object mocking framework
ii  python3-httmock                  1.3.0-1                             all          Mocking library for python3-requests
ii  python3-mock                     2.0.0-4                             all          Mocking and Testing Library (Python3 version)
ii  ruby-minitest                    5.11.3-1                            all          Ruby test tools supporting TDD, BDD, mocking, and benchmarking

@samdoran
Copy link
Author

I updated the example with working code. I'm not sure exactly about the real output since I don't have a system to test on. If you feel it's necessary, we could also add more output examples and iterate through them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment