python-unittest

Failed to navigate to https://www.google.ca. This usually means that a call to the COM method IWebBrowser2::Navigate2() with Python Unittest Selenium

一曲冷凌霜 提交于 2021-02-11 12:44:50
问题 from selenium import webdriver import unittest from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from time import sleep class MyTestCase(unittest.TestCase): def setUp(self) -> None: self.driver = webdriver.Ie(executable_path="C:\\webdriver\\IEDriverServer.exe") self.driver.maximize_window() self.driver.get("https://www.google.ca") def test_googletest(self): element =

How can I mock any function which is not being called directly?

有些话、适合烂在心里 提交于 2021-02-10 15:17:00
问题 TL;DR How can I patch or mock "any functions that are not being called/used directly" ? Sceneario I have a simple unit-test snippet as # utils/functions.py def get_user_agents(): # sends requests to a private network and pulls data return pulled_data # my_module/tasks.py def create_foo(): from utils.functions import get_user_agents value = get_user_agents() # do something with value return some_value # test.py class TestFooKlass(unittest.TestCase): def setUp(self): create_foo() def test_foo

How can I mock any function which is not being called directly?

我的梦境 提交于 2021-02-10 15:15:25
问题 TL;DR How can I patch or mock "any functions that are not being called/used directly" ? Sceneario I have a simple unit-test snippet as # utils/functions.py def get_user_agents(): # sends requests to a private network and pulls data return pulled_data # my_module/tasks.py def create_foo(): from utils.functions import get_user_agents value = get_user_agents() # do something with value return some_value # test.py class TestFooKlass(unittest.TestCase): def setUp(self): create_foo() def test_foo

Patching decorator in setUp or setUpClass in TestCases does not work

廉价感情. 提交于 2021-02-10 13:36:30
问题 I am trying to patch some functions during either the setUp or setUpClass methods of a unittest.TestCase subclass. Given a module patch_me_not.py # patch_me_not.py def patch_me(at): print('I am not patched at {}.'.format(at)) def patch_me_not(at): patch_me(at) The following script produces more output that I would expect. # main.py import unittest from unittest.mock import patch from patch_me_not import patch_me_not @patch('patch_me_not.patch_me', lambda x: None) class PatchMeNotTests

How should I unit test MySQL queries?

我的未来我决定 提交于 2021-02-10 05:12:49
问题 I'm building some unit tests for my Python module which interfaces with a MySQL database via SQLAlchemy. From reading around I gather the best way to do this is to create a test database that I can query as if it was the real thing. I've done this however how should I test the existing queries in the module as they currently all point at the live database? The only idea I'd come up with was to do something like the following: def run_query(engine, db_name='live_db') engine.execute(f'SELECT *

django how to assert url pattern resolves to correct class based view function

送分小仙女□ 提交于 2021-02-08 12:40:30
问题 I have a class based view class HomePage(View): def get(self, request): return HttpResponse('<p>This is content.</p>') and url-pattern defined as below: urlpatterns = patterns('', url(r'^$', HomePage.as_view()), ) To this pattern resolves to current view function, I wrote a test like this: class HomePageTest(TestCase): def test_root_url_resolves_to_home_page_view(self): found = resolve('/') self.assertIsInstance(found.func, HomePage) By running this unittest I am getting following error: self

unittest, works locally, but not on a remote server, no module named x.__main__; 'x' is a package and cannot be directly executed

↘锁芯ラ 提交于 2021-02-08 05:58:40
问题 I am working on a Jenkins CI/CD pipeline for my Python package. My project file hierarchy is as follows: project/ - package_name - file1.py - file2.py - etc... - tests - unit - __main__.py - __init__.py - test1.py - test2.py All unit tests (I am using unittest ) are run using a single command python -m tests.unit via adding __init__.py of the following content: contents import os os.chdir(os.path.dirname(os.path.abspath(__file__))) and __main__.py which looks like this contents import

No test under python unittest in visual studio 2019

余生长醉 提交于 2021-01-28 18:40:53
问题 I have an existing python project in visual studio 2019 (Version16.3.9) containing unit test created for unittest.py under visual studio 2017. I already configered the project for unittest, because this is needed in visual studio 2019: But there are no unit tests shown. But I got the following error: File "C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\ENTERPRISE\COMMON7\IDE\EXTENSIONS\MICROSOFT\PYTHON\CORE\PythonFiles\testing_tools\run_adapter.py", line 17, in <module> tool, cmd,

python unit test patch mock method not returning the return values

时光总嘲笑我的痴心妄想 提交于 2021-01-28 05:42:30
问题 I am trying to write a test case test_is_user_present() which calls another function execute_redshift_sql() from redshift_util.py script I set the expected return value from the function execute_redshift_sql() to 1 . But I never get this value returned from result after the function is called ! I also printed some values for debugging purpose You can take a look at test case below from mock import patch, Mock, MagicMock from cia_admin_operations.redshift_util import execute_redshift_sql

How to permanently mock return value of a function in python unittest

半世苍凉 提交于 2021-01-27 17:45:57
问题 I have a function # foo.py NUM_SHARDS = 10 def get_shard(shard_key: int) -> int return (shard_key % NUM_SHARDS) + 1 I want to mock this function such that whenever this function is called, it returns a certain value. I've tried patching like such # test.py @mock.patch('foo.get_shard', return_value=11) def testSomething(self, patched_func): patched_func() # >> 11 ... OK so this is fine but get_shard(4) # >> 5 .... Original impl being executed I want to deeply modify this function to always