如何将变量从pytest函数导入另一个python脚本文件?

kd3sttzy  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(365)

我有下面的pytest脚本 Webtest.py 其中定义了要测试的url。

from seleniumwire import webdriver
import pytest
from selenium.webdriver.chrome.options import Options

class Test_main():

    @pytest.fixture()
    def test_setup(self):

        # initiating browser
        chrome_options = Options()
        chrome_options.add_argument('--start-maximized')
        chrome_options.add_argument('--headless')

        self.driver = webdriver.Chrome(executable_path=r"drivers/chromedriver v86/chromedriver.exe",options=chrome_options)

        yield
        self.driver.close()
        self.driver.quit()
        print("Test Completed")

    def test_case01(self,test_setup):
        self.url='lifesciences.cactusglobal.com'
        self.driver.get(self.url)
        title=self.driver.title
        print(title)

我想使用 self.url 要在另一个python脚本中使用的上述脚本中的值 SSL_trial.py . 我像下面那样尝试过,但在执行之前就显示出错误。

from OpenSSL import SSL
from cryptography import x509
from cryptography.x509.oid import NameOID
import idna
from Test_Website_Security import Test_main  #imported the file which is saved in the same folder
url_here= Test_main.test_case02().url  #I tried to call the variable here

from socket import socket
from collections import namedtuple

HostInfo = namedtuple(field_names='cert hostname peername', typename='HostInfo')

HOSTS = [
    (url_here, 443)
]

def verify_cert(cert, hostname):
   cert.has_expired()

def get_certificate(hostname, port):
    hostname_idna = idna.encode(hostname)
    sock = socket()

    sock.connect((hostname, port))
    peername = sock.getpeername()
    ctx = SSL.Context(SSL.SSLv23_METHOD) 
    ctx.check_hostname = False
    ctx.verify_mode = SSL.VERIFY_NONE

    sock_ssl = SSL.Connection(ctx, sock)
    sock_ssl.set_connect_state()
    sock_ssl.set_tlsext_host_name(hostname_idna)
    sock_ssl.do_handshake()
    cert = sock_ssl.get_peer_certificate()
    crypto_cert = cert.to_cryptography()
    sock_ssl.close()
    sock.close()

    return HostInfo(cert=crypto_cert, peername=peername, hostname=hostname)

def get_alt_names(cert):
    try:
        ext = cert.extensions.get_extension_for_class(x509.SubjectAlternativeName)
        return ext.value.get_values_for_type(x509.DNSName)
    except x509.ExtensionNotFound:
        return None

def get_common_name(cert):
    try:
        names = cert.subject.get_attributes_for_oid(NameOID.COMMON_NAME)
        return names[0].value
    except x509.ExtensionNotFound:
        return None

def get_issuer(cert):
    try:
        names = cert.issuer.get_attributes_for_oid(NameOID.COMMON_NAME)
        return names[0].value
    except x509.ExtensionNotFound:
        return None

def print_basic_info(hostinfo):
    s = '''» {hostname} « … {peername}
    \tcommonName: {commonname}
    \tSAN: {SAN}
    \tissuer: {issuer}
    \tnotBefore: {notbefore}
    \tnotAfter:  {notafter}
    '''.format(
            hostname=hostinfo.hostname,
            peername=hostinfo.peername,
            commonname=get_common_name(hostinfo.cert),
            SAN=get_alt_names(hostinfo.cert),
            issuer=get_issuer(hostinfo.cert),
            notbefore=hostinfo.cert.not_valid_before,
            notafter=hostinfo.cert.not_valid_after
    )
    print(s)
    print(type(s))
    xyz=list(s.split("\t"))
    print(xyz)
    print(len(xyz))
    print(xyz[5])
    print(xyz[4])

def check_it_out(hostname, port):
    hostinfo = get_certificate(hostname, port)
    print_basic_info(hostinfo)

import concurrent.futures
if __name__ == '__main__':
    with concurrent.futures.ThreadPoolExecutor(max_workers=4) as e:
        for hostinfo in e.map(lambda x: get_certificate(x[0], x[1]), HOSTS):
            print_basic_info(hostinfo)

我不知道怎么做。我知道这很基本,但非常感谢您的帮助。

pxyaymoc

pxyaymoc1#

如果 test_case01 不知怎的,我可以重新开始 self.url 然后,您可能可以尝试以下解决方案:-
比方说 A.py 看起来像这样:-

def test_case01():
    url = 'lifesciences.cactusglobal.com'
    return url

如果您想使用来自的url test_case01 进入 B.py :-
您可能可以这样做:

from A import *

var = test_case01()
print(var)

相关问题