python-unittest

Execution order of python unitests by their declaration

浪子不回头ぞ 提交于 2020-07-10 06:43:44
问题 I'm using python unittests and selenium and in my code I have one test class with many testcases: class BasicRegression(unittest.TestCase): @classmethod def setUpClass(cls): cls.driver = webdriver.Chrome(executable_path=Data.driver) cls.driver.implicitly_wait(1) cls.driver.maximize_window() def testcase1_some_stuff(self): do_something() def testcase2_some_stuff(self): do_something() def testcase3_some_stuff(self): do_something() ... @classmethod def tearDownClass(cls): cls.driver.close() cls

How to properly mock private members of a class

陌路散爱 提交于 2020-07-09 02:39:45
问题 I am trying to write some unit tests for a method that depends on another private method. - As shown in the example below: def is_member_of(self, group_name): members = self.__get_group_members(group_name) The private method that I'd like to mock is __get_group_members ; I'd also like to mock the private attribute __user_id since it will be used in the is_member_of function (not shown in the example above). What I have so far: import unittest from unittest import mock class Test(unittest

How to properly mock private members of a class

落爺英雄遲暮 提交于 2020-07-09 02:38:49
问题 I am trying to write some unit tests for a method that depends on another private method. - As shown in the example below: def is_member_of(self, group_name): members = self.__get_group_members(group_name) The private method that I'd like to mock is __get_group_members ; I'd also like to mock the private attribute __user_id since it will be used in the is_member_of function (not shown in the example above). What I have so far: import unittest from unittest import mock class Test(unittest

Replacement for test case inheritance in pytest?

ぃ、小莉子 提交于 2020-07-08 21:31:49
问题 Background In Python's unittest framework, it is a fairly common idiom to use inheritance on a base set of tests to apply an entire set of tests to a new problem, and occasionally to add additional tests. A trivial example would be: from unittest import TestCase class BaseTestCase(TestCase): VAR = 3 def test_var_positive(self): self.assertGreaterEqual(self.VAR, 0) class SubTestCase(BaseTestCase): VAR = 8 def test_var_even(self): self.assertTrue(self.VAR % 2 == 0) Which, when run, runs 3 tests

Replacement for test case inheritance in pytest?

假装没事ソ 提交于 2020-07-08 21:29:35
问题 Background In Python's unittest framework, it is a fairly common idiom to use inheritance on a base set of tests to apply an entire set of tests to a new problem, and occasionally to add additional tests. A trivial example would be: from unittest import TestCase class BaseTestCase(TestCase): VAR = 3 def test_var_positive(self): self.assertGreaterEqual(self.VAR, 0) class SubTestCase(BaseTestCase): VAR = 8 def test_var_even(self): self.assertTrue(self.VAR % 2 == 0) Which, when run, runs 3 tests

Check if Timer.cancel is called in unit test

回眸只為那壹抹淺笑 提交于 2020-06-27 15:45:13
问题 I'm using the threading.Timer package to execute a method after x seconds. However, in some cases I want to execute this method earlier and cancel the timer (so it isn't called twice). How do I unit test this? I want to know if the timer has stopped so that the method is not called anymore. I am now using the following code, unfortunately the is_alive still returns True from threading import Timer Class X(): def __init__(self, timeout): self.timer = Timer(timeout, self.some_method) self.timer

ResourceWarning unclosed socket in Python 3 Unit Test

青春壹個敷衍的年華 提交于 2020-06-24 08:02:28
问题 I'm modifying some code to be compatible between Python 2 and Python 3 , but have observed a warning in unit test output. /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py:601: ResourceWarning: unclosed socket.socket fd=4, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('1.1.2.3', 65087), raddr=('5.8.13.21', 8080) A little research determined this was also happening from popular libraries like requests and boto3. I could ignore the

No tests discovered when using VSCode, python and absolute imports

假如想象 提交于 2020-06-16 20:10:08
问题 I am trying to add unit tests to my python project and I can't get VS Code to discover my test. The problem is when I try to import the class that I am testing. If I try to run the test file, it passes. If I omit the from A.myfile import MyFile the test is discovered. Question : What am I doing wrong here? File structure : root ├── A │ ├── __init__.py │ └── myfile.py └── tests ├── __init__.py └── A ├── __init__.py └── test_myfile.py myfile.py : class MyFile(object): def __init__(self): self

No tests discovered when using VSCode, python and absolute imports

自古美人都是妖i 提交于 2020-06-16 20:04:51
问题 I am trying to add unit tests to my python project and I can't get VS Code to discover my test. The problem is when I try to import the class that I am testing. If I try to run the test file, it passes. If I omit the from A.myfile import MyFile the test is discovered. Question : What am I doing wrong here? File structure : root ├── A │ ├── __init__.py │ └── myfile.py └── tests ├── __init__.py └── A ├── __init__.py └── test_myfile.py myfile.py : class MyFile(object): def __init__(self): self

Why am I getting “No tests discovered” with Python tests in VS Code?

谁都会走 提交于 2020-06-16 17:31:24
问题 This is my first few lines of code but I've coded for 20 years so I quickly wanted to get unit testing running. I am using Windows 10 VS Code 1.30.2 from 7th January, 2019. Python 3.7.2 Python Extension ms-python.python 2018.12.1 Here's the contents of the folder I'm in. Directory: C:\DATA\Git\Py\my_first_code Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 19/01/2019 21:42 __pycache__ -a---- 19/01/2019 21:35 289 messing.py -a---- 19/01/2019 21:42 204 test_messing.py -a--