pytest

pytest 安装与入门

随声附和 提交于 2019-12-26 16:16:30
1.1 安装pytest 命令行执行以下命令 $ pip3 install -U pytest 检查版本 $ pytest --version This is pytest version 4.5.0, imported from /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pytest.py 1.2 创建你的第一个测试用例 创建一个简单的测试方法 #test_sample.py def func ( x ) : return x + 1 def test_a ( ) : print ( "---test_a----" ) assert func ( 3 ) == 5 #断言失败 def test_b ( ) : print ( "---test_b---" ) assert 1 #断言成功 执行一下 命令行模式 命令下执行 $ pytest -s test_sample.py 主函数模式 增加主函数 if __name__ == '__main__' : pytest . main ( [ "-s" , "test_sample.py" ] ) #-s 表示支持控制台打印,如果不加,print 不会出现任何内容 #-q 安静模式,不打印信息 执行结果 == == ==

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

supply an array full with objects as argument to another class and receive back the corrected

会有一股神秘感。 提交于 2019-12-25 18:36:34
问题 I want to do this. I have a class (MyInputs) where I will initialize the inputs. I will have a numpy array which will hold many instances of the class. For example, np.array([[MyInputs('31-01-2017 15:02:13.870916', 100)], [MyInputs('31-01-2017 15:02:13.870916', 20)]], dtype=object) I will then supply this array to another class (Criteria), make some computations and return back the corrected array. The code : testStack.py import numpy as np class MyInputs(): def __init__(self, timestamp,

How to use python3 setup.py test with PyTest and PyQt5?

扶醉桌前 提交于 2019-12-25 17:36:30
问题 Is it possible to follow Good Integration as here http://pytest.org/latest/goodpractices.html in a package that depends on PyQt5 ? (I am using Ubuntu 14.04, don't know if this impacts) I get the following with both suggested approaches: $ python3 setup.py test running test Searching for pyqt5 Reading https://pypi.python.org/simple/pyqt5/ No local packages or download links found for pyqt5 error: Could not find suitable distribution for Requirement.parse('pyqt5') I can pip3 install pyqt5 ,

pybuilder and pytest: cannot import source code when running tests

北战南征 提交于 2019-12-25 04:38:32
问题 so i have a project: <root> |- src |-main |-python |-data_merger |- common |- constans |- controller |- resources |- rest |-tests |-unittest |-integrationtest data_merger is marked as root (I am using Pycharm). This is part of my build file: @init def set_properties(project): project.set_property("dir_source_main_python", r"src\main\python\data_merger") project.set_property("dir_source_integrationtest_python", r"src\tests\integrationtest") project.set_property("dir_source_unittest_python", r

How to pass an environment variable in command line to pytest to test a function

假装没事ソ 提交于 2019-12-25 01:35:17
问题 I have a script using os.environ.get() to get variable from command line something like JENKINS_HOST="xx" JENKINS_AUTH="xx" JENKINS_TOKEN="xx" python script.py In script.py has a function it likes this: def init_auth(): login_mode = True JENKINS_HOST = os.environ.get("JENKINS_HOST") JENKINS_AUTH = os.environ.get("JENKINS_AUTH") JENKINS_TOKEN = os.environ.get("JENKINS_TOKEN") when I use pytest to test the function init_auth(), how could I transfer the cli environment to this function? 回答1: I'm

“file not found” on travis-ci when running pytest from tox using docker

穿精又带淫゛_ 提交于 2019-12-25 00:43:16
问题 I'm trying to run my tests throught travis-ci, but i receive "file not found tests" error. When i run local with same command everything is ok, but in travis i receive this error. I think its because tests folder which in root of my project located somewhere in other directory - in directory where travis copy github repo. I try this settings in tox.ini but none of whem help: commands = py.test $TRAVIS_BUILD_DIR/tests {posargs} passenv = TRAVIS_BUILD_DIR commands = py.test $TRAVIS_BUILD_DIR

passing more than one argument to instantiate more than one object in Pytest

让人想犯罪 __ 提交于 2019-12-24 20:47:44
问题 Related: Passing arguments to instantiate object in Pytest I have MyClass1 and 2 which get consumed by MyClass3. Like so: class MyClass1: def __init__(self, a): self.a = a class MyClass2: def __init__(self, b): self.b = b class MyClass3: def __init__(self, obj1, obj2): self.obj1 = obj1 self.obj2 = obj2 def joint_operation(self): return self.obj1.a + self.obj2.b I wish to test the iterations of the following setup: my_obj1 = MyClass1("a") my_obj2 = MyClass2("b") my_obj3 = MyClass3(my_obj1, my

Py.Test silently skips tests with errors in them using PyDev/Eclipse?

不羁的心 提交于 2019-12-24 20:18:28
问题 I've been looking in to using Py.Test to automate unit testing in some code I've been working on. I've discovered the following behavior: when a test that I've built has an error (that would otherwise cause the interpreter to barf), the testing framework seems to silently ignore the test altogether. I'm worried that, as I implement more tests, I'll mistake "this test had an error and didn't run" for "this test passed". Ideally, I'd like to hit a button in Eclipse and have a unit test fail if

Django Channels Postgres InterfaceError: connection already closed

为君一笑 提交于 2019-12-24 17:18:54
问题 I can't seem to wrap my head around the issue here. I'm writing tests for my Channel Consumers following the description in the docs. I'd normally use the Django default unittest but since Channels requires using pytest, I'm using it but will still like to maintain the class structure of writing tests. I had issues with writing the setup method similar to unittest which I would use to initialize the test attributes. I tried method fixtures, class fixtures using autouse=true, but none of them