Skip to content

Commit 780d5a1

Browse files
author
Siddharta Govindaraj
committed
Writing a custom test loader to load tests by attribute
1 parent 5da0924 commit 780d5a1

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

run_test.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
11
import unittest
22

3-
from stock_alerter.tests import test_stock
3+
4+
class AttribLoader(unittest.TestLoader):
5+
def __init__(self, attrib):
6+
self.attrib = attrib
7+
8+
def loadTestsFromModule(self, module, use_load_tests=False):
9+
return super().loadTestsFromModule(module, use_load_tests=False)
10+
11+
def getTestCaseNames(self, testCaseClass):
12+
test_names = super().getTestCaseNames(testCaseClass)
13+
filtered_test_names = [test
14+
for test in test_names
15+
if hasattr(getattr(testCaseClass, test),
16+
self.attrib)]
17+
return filtered_test_names
18+
419

520
if __name__ == "__main__":
21+
loader = AttribLoader("slow")
22+
test_suite = loader.discover(".")
623
runner = unittest.TextTestRunner()
7-
runner.run(test_stock.suite())
24+
runner.run(test_suite)

stock_alerter/tests/test_stock.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import sys
21
import unittest
32
import collections
43
from datetime import datetime, timedelta
@@ -13,7 +12,6 @@ def setUp(self):
1312
def test_price_of_a_new_stock_class_should_be_None(self):
1413
self.assertIsNone(self.goog.price)
1514

16-
@unittest.skipIf(sys.platform.startswith("win"), "skip on windows")
1715
def test_stock_update(self):
1816
"""An update should set the price on the stock object
1917
@@ -22,7 +20,8 @@ def test_stock_update(self):
2220
self.goog.update(datetime(2014, 2, 12), price=10)
2321
self.assertEqual(10, self.goog.price)
2422

25-
@unittest.skipUnless(sys.platform.startswith("win"), "only run on windows")
23+
test_stock_update.slow = True
24+
2625
def test_negative_price_should_throw_ValueError(self):
2726
with self.assertRaises(ValueError):
2827
self.goog.update(datetime(2014, 2, 13), -1)

0 commit comments

Comments
 (0)