python-unittest

Python unittest passing arguments to parent test class

我的梦境 提交于 2019-12-31 04:50:15
问题 I have a parent test class named as basetestcase() This is inherited by all the test classes class BaseTestCase(unittest.TestCase): driver = None browser = read from command line operatingSystem = read from command line url = read from command line @classmethod def setUpClass(cls): """ SetUp to initialize webdriver session, pages and other needed objects Returns: None """ # Get webdriver instance # Browser should be read from the arguments if browser == "iexplorer": cls.driver = webdriver.Ie(

Assert two variables are almost equal in python

て烟熏妆下的殇ゞ 提交于 2019-12-25 18:58:12
问题 Here are two variables: earnings_forecast , actual_earning (numerical variables) I want to assert if both these variables are equal with a difference of ±2% acceptable with respect to actual_earning variable. Suppose: earnings_forecast = 6 actual_earnings = 5.19 I cannot use assertEqual(earnings_forecast, actual_earnings) because it will try do an exact match, instead I want to assert both these variables are almost equal with ±2% difference acceptable. 回答1: You can use the new isclose

How to pass testCase value to next one with Pytest

北慕城南 提交于 2019-12-25 18:49:48
问题 import pytest def add(x): return x + 1 def sub(x): return x - 1 testData1 = [1, 2] testData2 = [3] class Test_math(object): @pytest.mark.parametrize('n', testData1) def test_add(self, n): result = add(n) testData2.append(result) <-------- Modify testData here assert result == 5 @pytest.mark.parametrize('n', testData2) def test_sub(self, n): result = sub(n) assert result == 3 if __name__ == '__main__': pytest.main() there are only 3 tests : Test_math.test_add[1] , Test_math.test_add[2] , Test

how to specify test specific setup and teardown in python unittest

丶灬走出姿态 提交于 2019-12-25 01:14:57
问题 I want to create unittest test with two different set up and tearDown methon in same class with two different test. each test will use its specific setUp and tearDown method in python unittest framework. could anyone help me. class processtestCase(unittest.TestCase): print "start the process test cases " def setUp1(self): unittest.TestCase.setUp(self) def test_test1(self): "test Functinality" def tearDown1(self): unittest.TestCase.tearDown(self) def setUp2(self): unittest.TestCase.setUp2(self

How do I patch a sys attribute using a decorator?

旧时模样 提交于 2019-12-24 21:26:22
问题 I have a function which depends on the Python version. I would like to test this in a unittest by mocking sys.version info. The following code works: def test_python_version(self): with patch("sys.version_info", [3]): my_func() with patch("sys.version_info", [2]): my_func() I would like to use a decorator, but the following code does not work. Why is this? How do I set the value of the MagicMock object that gets passed into my test? Thanks @patch("sys.version_info") def test_python_version

Concurrency issue with psycopg2, Redshift, and unittest

▼魔方 西西 提交于 2019-12-24 16:35:15
问题 I am in Python 2.7, using psycopg2 to connect to an Amazon Redshift database. I have unit tests, and in the setUp and tearDown methods for this test class, I drop the tables that were created for the purpose of this test. So the scheme is: def setUp(self): drop_specific_tables() create_specific_tables() def tearDown(self): drop_specific_tables() The reason for dropping in the setUp as well as tearDown is in case a test exits unsafely and skips tearDown we can still know that whenever it runs

How do I write a unit test for OSError?

大憨熊 提交于 2019-12-24 14:25:15
问题 I have the following python code which I want to test: def find_or_make_logfolder(self): if not path.isdir(self.logfolder): try: makedirs(self.logfolder) except OSError: if not path.isdir(self.logfolder): raise I want to do something like the following in my unittest. def test_find_or_make_logfolder_pre_existing(self): with self.assertRaises(OSError): makedirs(self.logfolder) find_or_make_logfolder() However, if not path.isdir(self.logfolder): is checking if the directory already exists or

Python unit tests are not running at all

余生长醉 提交于 2019-12-24 12:14:59
问题 I am first time trying out python unit tests referring to this article. I have PyDev plugin installed in my Eclipse. My test_hello.py looks like this: import unittest class TestHello(unittest.TestCase): def test_abc(self): print("Test!!!") result = True self.assertEqual(result, True, "ohno") When I Right click on source > Run As > Python unit-test , it outputs: Finding files... done. Importing test modules ... PYTHONPATH not found for file: D:\workspaces\python-ws\test\test_h done. ----------

testing.postgresql command not found: initdb inside docker

笑着哭i 提交于 2019-12-24 07:41:23
问题 Hi i'm trying to make a unittest with postgresql database that use sqlalchemy and alembic Also im running it on docker postgresql I'm following the docs of testing.postgresql(docs) to set up a temporary postgresql instance handle database testing and wrote the the following test: def test_crawl_bds_obj(self): with testing.postgresql.Postgresql() as postgresql: engine.create_engine(postgresql.url()) result = crawl_bds_obj(1 ,'/ban-can-ho-chung-cu-duong-mai-chi-tho-phuong-an-phu-prj-the-sun

argparse fails when called from unittest test

可紊 提交于 2019-12-24 04:33:15
问题 In a file (say parser.py ) I have: import argparse def parse_cmdline(cmdline=None): parser = argparse.ArgumentParser() parser.add_argument('--first-param',help="Does foo.") parser.add_argument('--second-param',help="Does bar.") if cmdline is not None: args = parser.parse_args(cmdline) else: args = parser.parse_args() return vars(args) if __name__=='__main__': print parse_cmdline() Sure enough, when called from the command line it works and give me pretty much what I expect: $ ./parser.py -