From 9b205d110ef3542aeca06ad72335665c250f61ff Mon Sep 17 00:00:00 2001 From: gonghuihui55 Date: Thu, 21 Dec 2017 00:15:42 +0800 Subject: [PATCH 1/3] update conftest.py and insert test_module.py,test_smtpsimple.py --- studyPyTest/conftest.py | 16 +++++++++++----- studyPyTest/test_module.py | 15 +++++++++++++++ studyPyTest/test_smtpsimple.py | 17 +++++++++++++++++ 3 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 studyPyTest/test_module.py create mode 100644 studyPyTest/test_smtpsimple.py diff --git a/studyPyTest/conftest.py b/studyPyTest/conftest.py index 4a39b99..4079e6d 100644 --- a/studyPyTest/conftest.py +++ b/studyPyTest/conftest.py @@ -4,10 +4,10 @@ # @Author : gonghuihui # @File : conftest.py import pytest - +import smtplib # content of conftest.py -""" + @pytest.fixture(scope='session') def image_file(tmpdir_factory): img = compute_expensive_image() @@ -19,9 +19,9 @@ def image_file(tmpdir_factory): def test_histogram(image_file): img = load_image(image_file) # compute and test histogram -""" -""" + + def test_func_fact(): print("fast") @@ -32,10 +32,16 @@ def test_func_slow_1(): @pytest.mark.xfail(not pytest.config.getoption("--runslow")) def test_func_slow_2(): print("xfail slow") -""" + from test_foocompare import Foo def pytest_assertrepr_compare(op, left, right): if isinstance(left, Foo) and isinstance(left, Foo) and op == "==": return ['Comparing Foo instance:', ' vals: %s != %s' %(left.val, right)] + + +# content of test_module +@pytest.fixture(scope="module") +def smtp(): + return smtplib.SMTP("smtp.gmail.com", 587, timeout=5) \ No newline at end of file diff --git a/studyPyTest/test_module.py b/studyPyTest/test_module.py new file mode 100644 index 0000000..97852d0 --- /dev/null +++ b/studyPyTest/test_module.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Time : 17-12-20 下午11:59 +# @Author : gonghuihui +# @File : test_module.py +def test_ehlo(smtp): + respone, msg = smtp.ehlo() + assert respone == 250 + assert b"smtp.gmail.com" in msg + assert 0 #for demo purpose 啥意思 + +def test_noop(smtp): + respone, msg = smtp.ehlo() + assert respone == 250 + assert 0 #for demo purpose \ No newline at end of file diff --git a/studyPyTest/test_smtpsimple.py b/studyPyTest/test_smtpsimple.py new file mode 100644 index 0000000..5e52000 --- /dev/null +++ b/studyPyTest/test_smtpsimple.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Time : 17-12-20 下午10:46 +# @Author : gonghuihui +# @File : test_smtpsimple.py +import pytest + +@pytest.fixture() +def smtp(): + import smtplib + return smtplib.SMTP("smtp.gmail.com", 587, timeout=10) + +def test_ehlo(smtp): + response, msg = smtp.ehlo() + assert response == 250 + assert 0 +# 总是超时,为啥子嘞 \ No newline at end of file From 0c9759eba3db0bac4b47689dfc462b7640ce9c50 Mon Sep 17 00:00:00 2001 From: gonghuihui55 Date: Thu, 21 Dec 2017 23:36:50 +0800 Subject: [PATCH 2/3] insert test_anothersmtp.py test_yield2.py --- studyPyTest/test_anothersmtp.py | 9 +++++++++ studyPyTest/test_yield2.py | 12 ++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 studyPyTest/test_anothersmtp.py create mode 100644 studyPyTest/test_yield2.py diff --git a/studyPyTest/test_anothersmtp.py b/studyPyTest/test_anothersmtp.py new file mode 100644 index 0000000..86ff55a --- /dev/null +++ b/studyPyTest/test_anothersmtp.py @@ -0,0 +1,9 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Time : 17-12-21 下午11:29 +# @Author : gonghuihui +# @File : test_anothersmtp.py + +smtpserver = "mail.python.org" +def test_showhelo(smtp): + assert 0,smtp.helo() \ No newline at end of file diff --git a/studyPyTest/test_yield2.py b/studyPyTest/test_yield2.py new file mode 100644 index 0000000..d572caa --- /dev/null +++ b/studyPyTest/test_yield2.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Time : 17-12-21 下午10:48 +# @Author : gonghuihui +# @File : test_yield2.py +import pytest +import smtplib + +@pytest.fixture(scope="module") +def smtp(): + with smtplib.SMTP("smtp.gmail.com", 587, timeout=5) as smtp: + yield smtp #provide the fixture value From 96ec3ecaf9a9b80238848c294d131541df691187 Mon Sep 17 00:00:00 2001 From: gonghuihui55 Date: Thu, 21 Dec 2017 23:37:44 +0800 Subject: [PATCH 3/3] update conftest.py --- studyPyTest/conftest.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/studyPyTest/conftest.py b/studyPyTest/conftest.py index 4079e6d..89fcbd6 100644 --- a/studyPyTest/conftest.py +++ b/studyPyTest/conftest.py @@ -7,7 +7,7 @@ import smtplib # content of conftest.py - +""" @pytest.fixture(scope='session') def image_file(tmpdir_factory): img = compute_expensive_image() @@ -21,7 +21,6 @@ def test_histogram(image_file): # compute and test histogram - def test_func_fact(): print("fast") @@ -33,15 +32,20 @@ def test_func_slow_1(): def test_func_slow_2(): print("xfail slow") - +""" from test_foocompare import Foo def pytest_assertrepr_compare(op, left, right): - if isinstance(left, Foo) and isinstance(left, Foo) and op == "==": + if isinstance(left, Foo) and isinstance(right, Foo) and op == "==": return ['Comparing Foo instance:', ' vals: %s != %s' %(left.val, right)] # content of test_module @pytest.fixture(scope="module") -def smtp(): - return smtplib.SMTP("smtp.gmail.com", 587, timeout=5) \ No newline at end of file +def smtp(request): + server = getattr(request.module, "smtpserver", "smtp.mail.com") + smtp = smtplib.SMTP("smtp.gmail.com", 587, timeout=5) + yield smtp + print("finalizing %s (%s)" %(smtp, server)) + smtp.close() +