How to use test_command_test method in molecule

Best Python code snippet using molecule_python

test_command_test.py

Source:test_command_test.py Github

copy

Full Screen

1import os2import re3import sys4import shutil5import unittest as ut16import packaging.database7from os.path import join8from operator import getitem, setitem, delitem9from packaging.command.build import build10from packaging.tests import unittest11from packaging.tests.support import (TempdirManager, EnvironRestorer,12 LoggingCatcher)13from packaging.command.test import test14from packaging.command import set_command15from packaging.dist import Distribution16EXPECTED_OUTPUT_RE = r'''FAIL: test_blah \(myowntestmodule.SomeTest\)17----------------------------------------------------------------------18Traceback \(most recent call last\):19 File ".+/myowntestmodule.py", line \d+, in test_blah20 self.fail\("horribly"\)21AssertionError: horribly22'''23here = os.path.dirname(os.path.abspath(__file__))24class MockBuildCmd(build):25 build_lib = "mock build lib"26 command_name = 'build'27 plat_name = 'whatever'28 def initialize_options(self):29 pass30 def finalize_options(self):31 pass32 def run(self):33 self._record.append("build has run")34class TestTest(TempdirManager,35 EnvironRestorer,36 LoggingCatcher,37 unittest.TestCase):38 restore_environ = ['PYTHONPATH']39 def setUp(self):40 super(TestTest, self).setUp()41 self.addCleanup(packaging.database.clear_cache)42 new_pythonpath = os.path.dirname(os.path.dirname(here))43 pythonpath = os.environ.get('PYTHONPATH')44 if pythonpath is not None:45 new_pythonpath = os.pathsep.join((new_pythonpath, pythonpath))46 os.environ['PYTHONPATH'] = new_pythonpath47 def assert_re_match(self, pattern, string):48 def quote(s):49 lines = ['## ' + line for line in s.split('\n')]50 sep = ["#" * 60]51 return [''] + sep + lines + sep52 msg = quote(pattern) + ["didn't match"] + quote(string)53 msg = "\n".join(msg)54 if not re.search(pattern, string):55 self.fail(msg)56 def prepare_dist(self, dist_name):57 pkg_dir = join(os.path.dirname(__file__), "dists", dist_name)58 temp_pkg_dir = join(self.mkdtemp(), dist_name)59 shutil.copytree(pkg_dir, temp_pkg_dir)60 return temp_pkg_dir61 def safely_replace(self, obj, attr,62 new_val=None, delete=False, dictionary=False):63 """Replace a object's attribute returning to its original state at the64 end of the test run. Creates the attribute if not present before65 (deleting afterwards). When delete=True, makes sure the value is del'd66 for the test run. If dictionary is set to True, operates of its items67 rather than attributes."""68 if dictionary:69 _setattr, _getattr, _delattr = setitem, getitem, delitem70 def _hasattr(_dict, value):71 return value in _dict72 else:73 _setattr, _getattr, _delattr, _hasattr = (setattr, getattr,74 delattr, hasattr)75 orig_has_attr = _hasattr(obj, attr)76 if orig_has_attr:77 orig_val = _getattr(obj, attr)78 if delete is False:79 _setattr(obj, attr, new_val)80 elif orig_has_attr:81 _delattr(obj, attr)82 def do_cleanup():83 if orig_has_attr:84 _setattr(obj, attr, orig_val)85 elif _hasattr(obj, attr):86 _delattr(obj, attr)87 self.addCleanup(do_cleanup)88 def test_runs_unittest(self):89 module_name, a_module = self.prepare_a_module()90 record = []91 a_module.recorder = lambda *args: record.append("suite")92 class MockTextTestRunner:93 def __init__(*_, **__):94 pass95 def run(_self, suite):96 record.append("run")97 self.safely_replace(ut1, "TextTestRunner", MockTextTestRunner)98 dist = Distribution()99 cmd = test(dist)100 cmd.suite = "%s.recorder" % module_name101 cmd.run()102 self.assertEqual(record, ["suite", "run"])103 def test_builds_before_running_tests(self):104 self.addCleanup(set_command, 'packaging.command.build.build')105 set_command('packaging.tests.test_command_test.MockBuildCmd')106 dist = Distribution()107 dist.get_command_obj('build')._record = record = []108 cmd = test(dist)109 cmd.runner = self.prepare_named_function(lambda: None)110 cmd.ensure_finalized()111 cmd.run()112 self.assertEqual(['build has run'], record)113 @unittest.skip('needs to be written')114 def test_works_with_2to3(self):115 pass116 def test_checks_requires(self):117 dist = Distribution()118 cmd = test(dist)119 phony_project = 'ohno_ohno-impossible_1234-name_stop-that!'120 cmd.tests_require = [phony_project]121 cmd.ensure_finalized()122 logs = self.get_logs()123 self.assertIn(phony_project, logs[-1])124 def prepare_a_module(self):125 tmp_dir = self.mkdtemp()126 sys.path.append(tmp_dir)127 self.addCleanup(sys.path.remove, tmp_dir)128 self.write_file((tmp_dir, 'packaging_tests_a.py'), '')129 import packaging_tests_a as a_module130 return "packaging_tests_a", a_module131 def prepare_named_function(self, func):132 module_name, a_module = self.prepare_a_module()133 a_module.recorder = func134 return "%s.recorder" % module_name135 def test_custom_runner(self):136 dist = Distribution()137 cmd = test(dist)138 record = []139 cmd.runner = self.prepare_named_function(140 lambda: record.append("runner called"))141 cmd.ensure_finalized()142 cmd.run()143 self.assertEqual(["runner called"], record)144 def prepare_mock_ut2(self):145 class MockUTClass:146 def __init__(*_, **__):147 pass148 def discover(self):149 pass150 def run(self, _):151 pass152 class MockUTModule:153 TestLoader = MockUTClass154 TextTestRunner = MockUTClass155 mock_ut2 = MockUTModule()156 self.safely_replace(sys.modules, "unittest2",157 mock_ut2, dictionary=True)158 return mock_ut2159 def test_gets_unittest_discovery(self):160 mock_ut2 = self.prepare_mock_ut2()161 dist = Distribution()162 cmd = test(dist)163 self.safely_replace(ut1.TestLoader, "discover", lambda: None)164 self.assertEqual(cmd.get_ut_with_discovery(), ut1)165 del ut1.TestLoader.discover166 self.assertEqual(cmd.get_ut_with_discovery(), mock_ut2)167 def test_calls_discover(self):168 self.safely_replace(ut1.TestLoader, "discover", delete=True)169 mock_ut2 = self.prepare_mock_ut2()170 record = []171 mock_ut2.TestLoader.discover = lambda self, path: record.append(path)172 dist = Distribution()173 cmd = test(dist)174 cmd.run()175 self.assertEqual([os.curdir], record)176def test_suite():177 return unittest.makeSuite(TestTest)178if __name__ == "__main__":...

Full Screen

Full Screen

bot_test.py

Source:bot_test.py Github

copy

Full Screen

...83 result = runner.invoke(init_db, [config_path])84 assert result.exit_code == 085 86@pytest.mark.asyncio87async def test_command_test(bot):88 """89 Tests the !test command.90 91 * bot (fixture) - configured testable bot object92 """93 msg = MockMessage(1, "!test")94 await bot._parse_command(msg)95 96@pytest.mark.asyncio97async def test_command_addcourse(bot):98 """99 Tests the !addcourse command. Verifies that a message is sent to a mock100 channel, and that database entry for the course is created.101 ...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run molecule automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful