pytest-robotframework icon indicating copy to clipboard operation
pytest-robotframework copied to clipboard

script to convert robot tests to python

Open DetachHead opened this issue 2 years ago • 1 comments

  • [ ] imports
    • [X] Library imports
    • [ ] aliased Library imports
    • [ ] Resource imports
    • [ ] don't use * imports aka resolve imported keywords (hard)
  • [ ] control flow
    • [ ] if statements
    • [ ] for loops
    • [ ] while loops
    • [ ] try/except
  • [ ] fix cli output
  • [ ] keyword -> function calls
    • [X] args
    • [ ] kwargs
    • [ ] keyword definitions
    • [ ] resolve keywords and convert types instead of assuming str on everything (hard)
  • [ ] special cased builtin keywords
    • [X] no operation -> ...
    • [ ] log -> logger.info/logger.warn/etc
    • [ ] run keyword and ___ error -> try/except
  • [ ] tags -> markers
  • [ ] docstrings
    • [ ] module level (suite)
    • [ ] package level/init file or whatever (suite)
    • [ ] keywords
    • [ ] tests
  • [ ] variables
    • [ ] normal
    • [ ] list
    • [ ] dict

DetachHead avatar Oct 26 '23 21:10 DetachHead

chatgpt seems to be pretty good at doing this in my experience. here's an example prompt and the response it gave me:

prompt

convert the following robotframework code to python using pytest.

*** Settings ***
Documentation     A test suite for valid login.
...
...               Keywords are imported from the resource file
Resource          keywords.resource
Default Tags      positive

*** Test Cases ***
Login User with Password
    Connect to Server
    Login User            ironman    1234567890
    Verify Valid Login    Tony Stark
    [Teardown]    Close Server Connection

Denied Login with Wrong Password
    [Tags]    negative
    Connect to Server
    Run Keyword And Expect Error    *Invalid Password    Login User    ironman    123
    Verify Unauthorised Access
    [Teardown]    Close Server Connection

when converting keywords, import keyword from the pytest_robotframework module and decorate them with @keyword

answer

To convert the given Robot Framework code to Python using pytest and the pytest_robotframework module, you can follow the steps below. Note that the pytest_robotframework module is not a standard module, and I'm assuming you have a module with that name or a similar one that provides the necessary functionality.

  1. Install the pytest and pytest_robotframework modules if you haven't already:

    pip install pytest pytest_robotframework
    
  2. Create a Python module, for example, test_login.py, and write the following code:

    import pytest
    from pytest_robotframework import keyword
    
    @keyword
    def connect_to_server():
        # Implementation for connecting to the server
        pass
    
    @keyword
    def login_user(username, password):
        # Implementation for logging in a user
        pass
    
    @keyword
    def verify_valid_login(expected_username):
        # Implementation for verifying a valid login
        pass
    
    @keyword
    def verify_unauthorized_access():
        # Implementation for verifying unauthorized access
        pass
    
    @keyword
    def close_server_connection():
        # Implementation for closing the server connection
        pass
    
    @pytest.mark.positive
    def test_login_user_with_password():
        connect_to_server()
        login_user("ironman", "1234567890")
        verify_valid_login("Tony Stark")
        close_server_connection()
    
    @pytest.mark.negative
    def test_denied_login_with_wrong_password():
        connect_to_server()
        with pytest.raises(Exception, match="*Invalid Password"):
            login_user("ironman", "123")
        verify_unauthorized_access()
        close_server_connection()
    
  3. Run the tests using pytest:

    pytest test_login.py
    

This example assumes that you have implemented the actual functionality for connecting to the server, logging in a user, verifying a valid login, verifying unauthorized access, and closing the server connection. Replace the placeholder comments with the actual implementation according to your application.

DetachHead avatar Nov 25 '23 12:11 DetachHead