2

列出了我的 python 脚本:

===========================================

class ExampleTestCase(unittest.TestCase):
    capabilities = None

def setUp(self):
    self.driver = webdriver.Remote(desired_capabilities={ "browserName": broswer,      "platform": platform, "node": node })

def test_example(self):
    self.driver.get("www.360logica.com")
    self.assertEqual(self.driver.title, "360logica")

def tearDown(self):
    self.driver.quit()

if __name__ == "__main__":
    #unittest.main()
    args = sys.argv
    port = args[1]
    platform = args[2]
    broswer = args[3]
    suite = unittest.TestSuite()
    suite.addTest(ExampleTestCase("test_example"))
    runner = XMLTestRunner(file('results_ExampleTestCase_%s.xml' % (broswer), "w"))
    runner.run(suite)

===============================================

运行命令为:

$ ./python.exe Grid_1.py 5555 WINDOW firefox

===============================================

构建错误日志是:

$ ./python.exe Grid_1.py 5555 WINDOW firefox
Traceback (most recent call last):
      File "Grid_1.py", line 31, in <module>
        suite.addTest(ExampleTestCase("test_example"))
      File "C:\Python27\Lib\unittest\case.py", line 191, in __init__
        (self.__class__, methodName))
ValueError: no such test method in <class '__main__.ExampleTestCase'>: test_example

==================================================== =

请帮我。我对那个构建错误感到非常头疼,不知道如何修复它。


将带有正则表达式的 Javascript 转换为 C#

我正在尝试将一段 Javascript 转换为 .NET,但我似乎无法完全正确。

这是一个在 Express for NodeJs 中调用的方法。它将路径转换/test/:value1/:value2为可以在 URL 的一部分上使用的正则表达式。

/**
* Normalize the given path string,
* returning a regular expression.
*
* An empty array should be passed,
* which will contain the placeholder
* key names. For example "/user/:id" will
* then contain ["id"].
*
* @param {String|RegExp|Array} path
* @param {Array} keys
* @param {Boolean} sensitive
* @param {Boolean} strict
* @return {RegExp}
* @api private
*/

exports.pathRegexp = function(path, keys, sensitive, strict) {
  if (path instanceof RegExp) return path;
  if (Array.isArray(path)) path = '(' + path.join('|') + ')';
  path = path
    .concat(strict ? '' : '/?')
    .replace(/\/\(/g, '(?:/')
    .replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?(\*)?/g, function(_, slash, format, key, capture, optional, star){
      keys.push({ name: key, optional: !! optional });
      slash = slash || '';
      return ''
        + (optional ? '' : slash)
        + '(?:'
        + (optional ? slash : '')
        + (format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)')) + ')'
        + (optional || '')
        + (star ? '(/*)?' : '');
    })
    .replace(/([\/.])/g, '\\$1')
    .replace(/\*/g, '(.*)');
  return new RegExp('^' + path + '$', sensitive ? '' : 'i');
}

我尝试将其转换为:

private Regex ConvertPathToRegex(string path, bool strict, out List<string> keys) {

    keys = new List<string>();

    List<string> tempKeys = new List<string>();

    string tempPath = path;

    if (strict)
        tempPath += "/?";

    tempPath = Regex.Replace(tempPath, @"/\/\(", delegate(Match m) {
        return "(?:/";
    });

    tempPath = Regex.Replace(tempPath, @"/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?(\*)?", delegate(Match m) {

        string slash = (!m.Groups[1].Success) ? "" : m.Groups[1].Value;
        bool formatSuccess = m.Groups[2].Success;
        string format = (!m.Groups[2].Success) ? "" : m.Groups[2].Value;
        string key = m.Groups[3].Value;
        bool captureSuccess = m.Groups[4].Success;
        string capture = m.Groups[4].Value;
        bool optional = m.Groups[5].Success;
        bool star = m.Groups[6].Success;

        tempKeys.Add(key);

        string expression = "/";
        expression += (optional ? "" : slash);
        expression += "(?:";
        expression += (optional ? slash : "");
        expression += (formatSuccess ? format : "");
        expression += (captureSuccess ? capture : (formatSuccess ? format + "([^/.]+?" : "([^/]+?)")) + ")";
        expression += (star ? "(/*)" : "");

        return expression;
    });

    tempPath = Regex.Replace(tempPath, @"/([\/.])", @"\$1");
    tempPath = Regex.Replace(tempPath, @"/\*", "(.*)");
    tempPath = "^" + tempPath + "$";

    keys.AddRange(tempKeys);

    return new Regex(tempPath, RegexOptions.Singleline | RegexOptions.IgnoreCase);
}

但问题是,这种方法不能正常工作。由于我不是正则表达式的超级明星,我想知道是否可以得到一些帮助。

当您还添加?param=1.

编辑:当我第一次从路径中删除查询参数时,它实际上工作得很好。

抱歉,问题的答案是从 URL 中删除查询参数。

4

1 回答 1

0

You have suite.addTest(ExampleTestCase("test_example")), but your def is outside the scope of the class (if it is indeed your indentation). Make sure that test_example is part of the class.

class ExampleTestCase(unittest.TestCase):
    capabilities = None

    def setUp(self):
        self.driver = webdriver.Remote(desired_capabilities={ "browserName": broswer, "platform": platform})

    def test_example(self):
        self.driver.get("www.360logica.com")
        self.assertEqual(self.driver.title, "360logica")

    def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    #unittest.main()
    args = sys.argv
    port = args[1]
    platform = args[2]
    broswer = args[3]
    suite = unittest.TestSuite()
    suite.addTest(ExampleTestCase("test_example"))
    runner = XMLTestRunner(file('results_ExampleTestCase_%s.xml' % (broswer), "w"))
    runner.run(suite)

python substring.py 5555 WINDOW firefox This ends up dumping the results (as expected) as results_ExampleTestCase_firefox.xml

于 2013-02-27T07:21:07.743 回答